ashnator has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have 2 files and I am taking the key values from one file and parsing in the other file for values. Now I am getting a problem of multiple values for the same key like this:-
File 1 Looks like this:- DAVID 31 DAVID 29 DAVID 41 File 2 looks like this:- >DAVID 40 40 40 40 40 40 40 40 40 33 40 40 40 40 40 40 40 40 40 40 40 40 40 4 +0 40 40 40 40 40 40 40 40 40 40 40 40 >DAVID 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 66 40 40 6 40 40 40 40 40 + 40 40 40 40 40 40 29 40 40 40 6 40 >DAVID 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 4 +0 40 40 99 40 40 40 40 40 40 4 40 40
How can I deal with this since Perl Hash array supports only one value for a single key.
Actually I am doing it like this ,e.g,. If David value in File 1 is 31 then I search it in File 2 for 31 position and if 31 position in File 2 is >=20. Then my result looks like this:-
RESULT FILE:- DAVID 31 40 DAVID 29 40 # This line is not printed bcoz hash can take only single + key DAVID 41 40 # This line is not printed bcoz hash can take only single + key

Can you please suggest me how should I change my code to accommodate this short coming ?
Here is my sample code:-
#!/usr/bin/perl -w use strict; my %href; my $fn = <STDIN>; open(FH, "$fn") || die "Cannot open file"; while (<FH>) { chomp($_); $href{$1} = $2 if $_ =~ /(\S+)\s+(\S+)/; } while (my ($key, $value) = each(%href)) { #print $key. ", ". $value."\n"; } open(FD,"<FileToBeParsedForValues.txt") || die("Can't open: $!"); $/ = '>'; while ( <FD> ) { chomp; next unless ( s{ \A (\S+) \s+ (?= \d ) }{}xms and exists( $href{$1 +} )); my $name = $1; my @numbers = split /\D+/; my $one_number = $numbers[$href{$name} - 1]; if ( $one_number >= 20 ) { print "$name\t\t$href{$name}\t$one_number\n"; } } close FD;

Replies are listed 'Best First'.
Re: Howto deal with multiple Hash key values
by gwadej (Chaplain) on Nov 06, 2008 at 01:52 UTC

    The solution to this is actually quite simple. The values in a hash must be scalars. References are scalars. So use array references as the hash values.

    Replace

    $href{$1} = $2 if $_ =~ /(\S+)\s+(\S+)/;

    with

    push @{$href{$1}}, $2 if $_ =~ /(\S+)\s+(\S+)/;

    You'll need a little more work to use the value data, but you haven't lost any of it.

    while (my ($key, $value) = each(%href)) { print "$key, (@{$value})\n"; }

    The rest of your code will need similar modifications to adapt to arrays of values.

    G. Wade
Re: Howto deal with multiple Hash key values
by repellent (Priest) on Nov 06, 2008 at 01:57 UTC
    Please read Perl Data Structures Cookbook.
    use warnings; use strict; my %hash; push @{ $hash{key1} }, 1, 2, 3; $hash{key2} = [ 4, 5, 6 ]; my $array_ref1 = $hash{key1}; print "$array_ref1->[2]\n"; # output: 3 my $array_ref2 = $hash{key2}; print "$array_ref2->[1]\n"; # output: 5
Re: Howto deal with multiple Hash key values
by toolic (Bishop) on Nov 06, 2008 at 02:04 UTC
    You could use HASHES OF ARRAYS:
    use strict; use warnings; use Data::Dumper; my %hoa; while (<DATA>) { my ($name, $num) = split; push @{$hoa{$name}}, $num; } print Dumper(\%hoa); __DATA__ DAVID 31 DAVID 29 DAVID 41

    prints:

    $VAR1 = { 'DAVID' => [ '31', '29', '41' ] };