in reply to Re: Two arrays into hash
in thread Two arrays into hash
[@array] can be/usually is written as \@array
Careful. The two are not the same. \@array returns a reference to the existing array. [@array] creates a new (anonymous) copy of the original array and returns a reference to the copy. It's a subtle difference, but an important one.
#!/usr/bin/perl use strict; use warnings; my @arr = (1 .. 5); my %hash = ( aref => \@arr, copy => [ @arr ], ); print "@{$hash{copy}}\n"; print "@{$hash{aref}}\n"; @arr = ('a' .. 'e'); print "@{$hash{copy}}\n"; # original values print "@{$hash{aref}}\n"; # changed values
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Two arrays into hash
by Bloodnok (Vicar) on Jul 16, 2009 at 10:43 UTC |
In Section
Seekers of Perl Wisdom