Alexandre Checinski am Donnerstag, 24. November 2005 15.06:
> Hey big thx for your help.
> it's exactly what I wanted.
> I'm still trying to figure out how it works but it does work.
>
> I don't undestand the line :
> my $s='$struct->'.(join '', map { '{'.$l->[$_].'}' } [EMAIL PROTECTED]).'=1;';
Sorry for that... here's an explanation:
With the line, I build strings like
$struct->{A}{B}{1}{2}=1;
which are evaled and executed at runtime. (btw, this is short for
$struct->{A}->{B}->{1}->{2}=1;) see perldoc -f eval
The line consists of
LITERAL . (...) . LITERAL.
I explain the (...) part, by writing it different:
join '', #1
map { #2
'{'.$l->[$_].'}' #3
} #4
[EMAIL PROTECTED]; #5
The statement is read from bottom to top:
#5 produces a list of integers from 0 to the highest index in the @$l array
($l is an arrayref and @$l dereferences it).
Now, the map of #2 makes a string for every of these integers:
#3 looks into the $l arrayref extracting the value at the index position, and
surrounds it with '{}'. $_ contains the actual integer/index of the list #5.
The result so far is a list of the form ('{X}', '{Y}',....) wich is joined
together by the join in #1.
see
perldoc -f join
perldoc -f map
perldoc perlvar
> *And If you want to know what I'm using it for here is an example.*
>
> this is the content of the file I use to create the lists : (a list is a
> line of the file splitted with coma as separator)
>
> ProcessingEngine,RoutingEngine,CGF
> ProcessingEngine,RoutingEngine,DCC
> ProcessingEngine,RoutingEngine,CFS
> EgressInterfaceName,NextHopAddress,EgrLabel
> ProcessingEngine,RoutingEngine,RADIUS
> InterfaceName
> ProcessingEngine,RoutingEngine,DHCP
> ProcessingEngine,RoutingEngine
> ProcessingEngine
> IngressInterfaceName,Vrid,IngrLabel
> ProcessingEngine,RoutingEngine,APOOL
> ProcessingEngine,RoutingEngine,Apn
>
> I need to have a hash in order to create the output file and also to
> store the hierarchy for further usage.
> this is the out the output I want to have : (this represent a hierarchy,
> the level is determined by the number of tabulations)
>
> 135001, MPE_ProcessingEngine
> 135002, MPE_RoutingEngine
> 135003, MPE_Apn
> 135004, MPE_APOOL
> 135005, MPE_CFS
> 135006, MPE_CGF
> 135007, MPE_DCC
> 135008, MPE_DHCP
> 135009, MPE_RADIUS
> 135010, MPE_Interface
> 135011, MPE_EgressInterface
> 135012, MPE_NextHopAddress
> 135013, MPE_EgrLabel
> 135014, MPE_IngressInterface
> 135015, MPE_Vrid
> 135016, MPE_IngrLabel
>
>
> I'm sure there is a better solution for this problem but I haven't found
> it. If you have an idea....
Possibly there's a better solution, don't know at the moment.
Anyway, to create the desired output, you could traverse the $struct nested
hash in a recursive way and sort every output level locally if desired.
greetings
joe
[OP shortened:]
> John Doe wrote:
> >Alexandre Checinski am Donnerstag, 24. November 2005 12.11:
> >>Hello everobody,
> >>I'm new to this list.
> >>
> >>I have a problem to solve that have been mesing with for quite a few
> >>days now.
> >>
> >>I have lists that looks like this :
> >>(A,B,C,1,5)
> >>(A,B,C,1,2)
> >>(A,B,2,1,2)
> >>(A,B,C,D,1)
> >>(A,B,Y)
> >>
> >>And I would like to get something like this :
> >>{
> >> A => {
[...]
> >#!/usr/bin/perl -w
> >
> >use strict;
> >use warnings;
> >use Data::Dumper;
> >
> >my @lists=(
> > [qw(A B C 1 5)],
> > [qw(A B C 1 2)],
> > [qw(A B 2 1 2)],
> > [qw(A B C D 1)],
> > [qw(A B Y)],
> >);
> >
> >my $struct;
> >for my $l (@lists) {
> > no strict 'vars';
> > my $s='$struct->'.(join '', map { '{'.$l->[$_].'}' } [EMAIL
> > PROTECTED]).'=1;';
> > eval $s or die;
> >}
> >warn Data::Dumper::Dumper ($struct);
> >
> >$VAR1 = {
> > 'A' => {
> > 'B' => {
> > 'C' => {
> > '1' => {
> > '2' => 1,
> > '5' => 1
> > },
> > 'D' => {
> > '1' => 1
> > }
> > },
> > 'Y' => 1,
> > '2' => {
> > '1' => {
> > '2' => 1
> > }
> > }
> > }
> > }
> > };
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>