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;

In reply to Multiple Hash key values problem !! by ashnator

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.