in reply to Trouble iterating through a hash

I don't really understand what in FILE2 is supposed to match with what in FILE1? All non-zero data in FILE1 is in some column in FILE2. You do not show a line in FILE2 that would not "match" and you do not provide a sample "good" output.

I started re-writing your code, but could not proceed to a final solution due to the above. However this code below may help you.

Some comments to your code:

#!/usr/bin/perl use warnings; use strict; use diagnostics; my %hash; #$damID, $damF, $damAHC, $prog my $damF = <<END; 501093 0 0 3162 2958 0 0 3163 1895 0 0 3164 1382 0 0 3165 2869 0 0 3166 END #$ID, $sire, $dam, $F, $FB, $AHC, $FA my $wholepedigree_F = <<END; 3162 2159 501093 0 0 0 0 3163 2960 2958 0 0 0 0 3164 2269 1895 0 0 0 0 3165 1393 1382 0 0 0 0 3166 2881 2869 0 0 0 0 END open (FILE1, '<', \$wholepedigree_F) or die "Couldn't open wholepedigr +ee_F.txt"; while (my $line = <FILE1>) { chomp $line; next unless $line =~ /\S/; #skip blank lines my ($ID, $F, $AHC) = (split (/\s+/, $line))[0,3,5]; $hash{$ID} = "$F\t$AHC"; } close FILE1; open (FILE2, '<', \$damF) or die "Can't open damF.txt"; #open (OUT, ">output.txt") or die "Can't Open output file"; #print OUT "\n"; while (my $line = <FILE2>) { chomp $line; next unless $line =~ /\S/; #skip blank lines my ($damID, $damF, $damAHC, $prog) = split (/\s+/, $line); if ($hash{$prog}) { my $info1 = $hash{$prog}; my $info2 = "$damID\t$damF\t$damAHC"; print "$prog\t$info1\t$info2\n"; } } #close OUT; close FILE2; print "Done";

Replies are listed 'Best First'.
Re^2: Trouble iterating through a hash
by huck (Prior) on Mar 09, 2017 at 05:03 UTC

    I have no idea about what you think this $N stuff does?

    I had a clue, er a guess, so i tested it. next if 1..$N==$.; skips the first line, kinda the hard way.

    I agree with your other points, except that as written $info2 = $hash2 -> {$prog} -> {info2}; doesnt hurt either, it does that when the -> is assumed in $info2 = $hash2 -> {$prog}{info2}; anyway

      I agree with you "skips the first line, kinda the hard way.".

      I would advise the OP to use:

      my $discard_first_line = <FILE1>;
      before starting the while loop.
      The more cryptic albeit simple: <FILE1>; would work also. I prefer the extraneous "my" variable because it is very "cheap" and it is clear what it does. I would personally write a comment like <FILE1>; #discard first line anyway.

      I agree with your other points, except that as written $info2 = $hash2 -> {$prog} -> {info2}; doesnt hurt either, it does that when the -> is assumed in $info2 = $hash2 -> {$prog}{info2}; anyway
      My point is that there is no need at all for a 2nd dimension on the first hash, and no need for the 2nd hash at all!