Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Array values into hash

by FireBird34 (Pilgrim)
on Feb 09, 2003 at 06:45 UTC ( #233849=perlquestion: print w/replies, xml ) Need Help??
FireBird34 has asked for the wisdom of the Perl Monks concerning the following question:

Given that FILE contains:


And so forth, how would I be able to place that into a hash, so that:

$hash{key1} = value1? I am a bit stuck here... Any input would be appreciated. Thanks.

Replies are listed 'Best First'.
Re: Array values into hash
by demerphq (Chancellor) on Feb 09, 2003 at 15:17 UTC
    Please be careful with some of the solutions posted here. While in principle they may work most suffer from one bad practice or another (mostly the fact that they are slurping in potentially huge files for no good reason).

    Here is a relatively "textbook" solution.

    my %hash; open my $fh,$file or die "$file : $!"; while (<$fh>) { my ($key,$value) = split /\|/; warn "$_ seems not to be formatted correctly" unless defined($key) and length($key); $hash{$key}=$value; }
    By using the while you only have one line in memory at once. So even though the hash grows you dont have multiple copys of the file in memory at once (even if they are in different forms. For instance, the map solution posted here actually holds the data in ram 3 times at one critical moment. Once in the input list, once as a list of elements to go into the hash and once in the hash itself)

    --- demerphq
    my friends call me, usually because I'm late....

Re: Array values into hash
by primus (Scribe) on Feb 09, 2003 at 06:57 UTC
    this should work...

    #!/usr/bin/perl use warnings; use strict; my (@lines,%hash); open (IN,"<data.txt") or die "cannot open file for reading: $!"; @lines = <IN>; close (IN) or die "cannot close file: $!"; foreach (@lines) { $_ =~ /(.*)\|(.*)\|/; $hash{$1} = $2; }
Re: Array values into hash
by Enlil (Parson) on Feb 09, 2003 at 07:04 UTC
    This would work (assuming all the lines are the way you specify).
    use strict; use warnings; #grabs the line splits it into two parts and #puts in hash my %hash = map { (split /\|/,$_)[0,1] } <DATA>; #print the hash foreach (keys %hash) { print "$_, $hash{$_}",$/;} __DATA__ key1|value1| key2|value2| key3|value3|


Re: Array values into hash
by ihb (Deacon) on Feb 09, 2003 at 17:09 UTC
    This question is quite similar to your question a couple of days ago...
Re: Array values into hash
by pg (Canon) on Feb 09, 2003 at 08:15 UTC
    The other way is to use eval:
    use Data::Dumper; use strict; undef $/; open(FILE, "<", "test.txt"); $_ = <FILE>; close(FILE); s/\|\n/\,/g;#may need to modify according to newline on your platform, + this is fine with win32 s/\|/=>/g; my $hash ; eval("\$hash = {$_}"); print Dumper($hash);
      I feel obliged to point out that this is an insane way to do this. The security implications, the fact that that you have undefed $/ without localization, the fact that you are potentially reading in a huge file to memory all make this a very bad solution.


      --- demerphq
      my friends call me, usually because I'm late....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://233849]
Approved by Paladin
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2017-05-24 11:18 GMT
Find Nodes?
    Voting Booth?