InfiniteSilence has asked for the wisdom of the Perl Monks concerning the following question:
--begin snip--
I was looking at your site ( www.sysarch.com ... hash_slices.txt) for good examples of hash slices when I came across the following code:
With the explanation:@array = qw( a b c d ) ; @array{ @array } = ( [ @array ] ) x @array ;
Actually this is FALSE. Your original code does not create four separate anonymous array references but four entries that point to the same hash reference. Debugging reveals the error:...this means the hash %array looks like this: %array = ( 'a' => [ 'a', 'b', 'c', 'd' ], 'b' => [ 'a', 'b', 'c', 'd' ], 'c' => [ 'a', 'b', 'c', 'd' ], 'd' => [ 'a', 'b', 'c', 'd' ], ) ;
And a test confirms this:DB<1> n main::(-e:1): @array = qw( a b c d ) ;@array{ @array } = ( + [ @array ] ) x @array ; print $array{'a'}->[1]; DB<1> main::(-e:1): @array = qw( a b c d ) ;@array{ @array } = ( + [ @array ] ) x @array ; print $array{'a'}->[1]; DB<1> x %array 0 'a' 1 ARRAY(0x1d7f550) 0 'a' 1 'b' 2 'c' 3 'd' 2 'b' 3 ARRAY(0x1d7f550) -> REUSED_ADDRESS 4 'c' 5 ARRAY(0x1d7f550) -> REUSED_ADDRESS 6 'd' 7 ARRAY(0x1d7f550) -> REUSED_ADDRESS DB<2>
--end snip--C:\>perl -e "@array = qw( a b c d ) ;@array{ @array } = ( [ @array ] ) x @array ; $array{'a'} +->[1] = 'FOOBAR'; print $array{'a'}->[1]; print $array{'b'}->[1];" FOOBARFOOBAR
It was a bummer that I could not use the syntax given to get a hash of anonymous arrays that are different. Here's my question, how do you get four anonymous arrays using similar syntax?
Celebrate Intellectual Diversity
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting different anonymous arrays
by broquaint (Abbot) on Sep 08, 2003 at 16:06 UTC | |
|
Re: Getting different anonymous arrays
by welchavw (Pilgrim) on Sep 08, 2003 at 19:56 UTC | |
|
Re: Getting different anonymous arrays
by flounder99 (Friar) on Sep 08, 2003 at 16:29 UTC | |
by broquaint (Abbot) on Sep 08, 2003 at 18:59 UTC | |
by MidLifeXis (Monsignor) on Sep 08, 2003 at 16:57 UTC | |
by flounder99 (Friar) on Sep 08, 2003 at 18:38 UTC |