ashnator has asked for the wisdom of the Perl Monks concerning the following question:
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:-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
Can you please suggest me how should I change my code to accommodate this short coming ? Here is my sample code:-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
#!/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 | |
|
Re: Howto deal with multiple Hash key values
by repellent (Priest) on Nov 06, 2008 at 01:57 UTC | |
|
Re: Howto deal with multiple Hash key values
by toolic (Bishop) on Nov 06, 2008 at 02:04 UTC |