RE: Unknown level of hash

[email protected] ("Zhuang Li")
Newsgroups perl.fwp
Message-ID <7A7034183C845E4B9963F65505BA34D121939D@vchqex01.wl.corp.valueclick.com>
Vladi,

I like the 'hidden loop' with map solution better. As you can probably
tell, just get:
$hash->{E1}->{E2}->...->{En} = 'v1' is not that meaningful but this is:
Given:
$a = [
		['E11', 'E12', ..., 'E1n'],
		['E21', 'E22', ..., 'E2n'],
		...
		['Em1', 'Em2', ..., 'Emn'],
	];

$b = ['V1', 'V2', ..., 'Vm'];


Get(without loops but hidden loop is OK):

$hash->{E11}->{E12}->...->{E1n} = 'V1';
$hash->{E21}->{E22}->...->{E2n} = 'V2';
...
$hash->{Em1}->{Em2}->...->{Emn} = 'Vm';


john


> -----Original Message-----
> From: Vladi Belperchinov-Shabanski [mailto:[email protected]]
> Sent: Monday, March 28, 2005 4:02 PM
> To: [email protected]
> Subject: Re: Unknown level of hash
> 
> 
> two solutions:
> 
> obvious one:
> 
>    @a = ( 'E1', 'E2', 'E3', 'En' );
>    eval '$a{ ' . join( ' }{ ', @a ) . '} = 1';
>    print Dumper( \%a );
> 
> Interpreters rule :)
> 
> and 'hidden loop' one:
> 
>    @a = ( 'E1', 'E2', 'E3', 'En' );
>    $a = 1;
>    map{ $a = { $_ => $a } } reverse @a;
>    print Dumper( $a );
> 
> both print this:
> 
> $VAR1 = {
>           'E1' => {
>                     'E2' => {
>                               'E3' => {
>                                         'En' => 1
>                                       }
>                             }
>                   }
>         };
> 
> cheers!
> 
> P! Vladi.
> 
> On Mon, 28 Mar 2005 15:06:41 -0800
> "Zhuang Li" <[email protected]> wrote:
> 
> > Hi, given an array: @a = ('E1', 'E2', ..., 'En');
> >
> >
> >
> > Is there an easy way, hopefully one liner, to do the following
without a
> > loop? If not, will Perl support this in Perl 6?
> >
> >
> >
> > $hash->{E1}->{E2}->...->{En} = 1;
> >
> >
> >
> >
> >
> > Thanks,
> >
> >
> >
> > john
> >
> >
> 
> 
> --
> Vladi Belperchinov-Shabanski <[email protected]> <[email protected]>
> http://cade.datamax.bg/   pgp/gpg key id: 6F35B214 (pgp.mit.edu)
> Cross the ocean deep, wake now from your sleep
> Kiss the past goodbye, don't follow empty skies...
>
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.