Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to copy an array to a hash?

by MaW (Initiate)
on Jan 11, 2008 at 09:26 UTC ( [id://661838]=note: print w/replies, xml ) Need Help??


in reply to How to copy an array to a hash?

I'm not sure what you're really trying to achieve with this, but let's make sure you know how arrays get assigned to hashes.
my @array = (1, 2, 3, 4, 5, 6); my %hash = @array;
Is the same as saying:
my %hash = ( 1 => 2, 3 => 4, 5 => 6 );
apart from not having an @array floating around in the second version of course. If you really want to get each non-empty line in your input file into a hash with a key that's generated by some iterative process, you want something like
open my $file, '<', $filename or die "Unable to open '$filename': $!"; my %edit; for (<$file>) { next if //; chomp; # Some code to get a $key with an appropriate value $edit{$key} = $_; } close $file;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-26 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found