Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Two arrays into hash

by davorg (Chancellor)
on Jul 16, 2009 at 10:40 UTC ( #780642=note: print w/ replies, xml ) Need Help??


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
--

See the Copyright notice on my home node.

Perl training courses


Comment on Re^2: Two arrays into hash
Download Code
Replies are listed 'Best First'.
Re^3: Two arrays into hash
by Bloodnok (Vicar) on Jul 16, 2009 at 10:43 UTC
    Good point, well made - I rather overlooked that didn't I ?

    Thanx davorg.

    A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://780642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2015-07-30 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls