below is a question i need help addressing. the script works in the way i want it to. i just need help finishing it. my script takes one file and creates a hash from it and then takes the second file and creates a key from it. the last part is suppose to take the keys that are the same and print out the corresponding value along with the key from the first file. I can get the key but not the value

heres and example of the data.

file 1

name1 ABCD

name2 BCD

name3 ASCDA

name4 AAAAA

file 2

name1 0.2

name5 0.3

name4 0.0002

name7 0.222

my %s; open(FILE1, $ARGV[0]) or die "Cannot open the file: $!"; my $hash_key; my $hash_value; while (my $line = <FILE1>) { if ($line =~ /^>(\S+)/) { $hash_key = $1; } elsif ($line =~ /\S/) { chomp $line; $hash_value = $line; $s{$hash_key} = $hash_value; } } foreach my $hash_key1 (keys %s) { print "$hash_key1\t$s{$hash_key1}\n"; } my %n; open(FILE2, $ARGV[1]) or die "Cannot open the file: $!"; my $hash_key2; my $hash_value2; while (my $line2 = <FILE2>) { if ($line2 =~ /(\S*)/) { $hash_key2 = $1; $hash_value2 = $line2; $n{$hash_key2} = $hash_value2; } elsif ($line2 =~ /\S/) { chomp $line2; } } foreach my $hash_key3 (keys %n) { print "$hash_key3\n"; } my @n = (); foreach (keys %n) { push(@n, $_) if exists $s{$_}; #this is where i am having trouble. tried using dumper and #grep didnt + help print "$_\n"; } close FILE1; close FILE2; exit;

help would be appreciated. cheers.


In reply to Retrieving Key and Value Hashes by gghelpneeded

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.