Re: Unknown level of hash

Суханов Вадим <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
В сообщении от 29 Март 2005 01:08 Alexandre Jousset написал(a):
>  Hey all...
>
> Vladi Belperchinov-Shabanski wrote:
> [snip...]
>
> > and 'hidden loop' one:
> >
> >    @a = ( 'E1', 'E2', 'E3', 'En' );
> >    $a = 1;
> >    map{ $a = { $_ => $a } } reverse @a;
> >    print Dumper( $a );
>
>  The problem with this one is that by doing this you 
> s. e.g. :
>
> @a = ( 'E1', 'E2', 'E3', 'En' );
> $a = 1;
^^^ destroy existing hash

> map{ $a = { $_ => $a } } reverse @a;
>
> @a = ( 'E1', 'Ex', 'Ey', 'Ez' );
> $a = 1;
> map{ $a = { $_ => $a } } reverse @a;
> print Dumper( $a );



use Data::Dumper;

@a = ( 'E1', 'E2', 'E3', 'En' );
$a = 1;
map{ $a = { $_ => $a } } reverse @a;

@a = ( 'E1', 'Ex', 'Ey', 'Ez' );
$b = \ $a->{E1}->{E3} ; # a new branch
map{ $$b = { $_ => $$b } } reverse @a;
print Dumper( $a );


gives:

$VAR1 = {
          'E1' => {
                    'E2' => {
                              'E3' => {
                                        'En' => 1
                                      }
                            },
                    'E3' => {
                              'E1' => {
                                        'Ex' => {
                                                  'Ey' => {
                                                            'Ez' => undef
                                                          }
                                                }
                                      }
                            }
                  }
        };

-- 
vad
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.