Hi e9292,

I rewrote your code without changing it too much so that it (a) works as specified and (b) gets rid of lots of unnecessary cruft, (c) primarily your use of use vars (deprecated) and global variables (discouraged).

However please note that you appear to be working with CSV-format files (tab-delimited), and so you would be much safer to read them in with a CSV-parsing module such as Text::CSV_XS which can handle empty fields, blank lines, special characters inside fields, etc.

Also, if you have 500,000 entries, I would suggest storing the output data in a simple RDBMS such as SQLite. This will make working with the data much easier after you collate it.

#!/usr/bin/perl use warnings; use strict; use autodie; my %whole_pedigree; open( my $in, '<', 'wholepedigree_F.txt'); while ( my $line = <$in> ) { chomp $line; my ( $ID, $sire, $dam, $F, $FB, $AHC, $FA ) = split ( /\s+/, $line + ); if ( $ID ) { $whole_pedigree{ $ID } = "$F\t$AHC"; } } close $in; open( my $look_for, '<', 'damF.txt' ); open( my $output, '>', 'output.txt' ); while ( my $line = <$look_for> ) { chomp $line; my ( $damID, $damF, $damAHC, $prog ) = split (/\s+/, $line); if ( $prog && $whole_pedigree{ $prog } ) { print $output join( "\t", $prog, $whole_pedigree{ $prog }, "$ +damID\t$damF\t$damAHC" ), "\n"; } } close $look_for; close $output; print "Done\n";
cat output.txt
3162 0 0 501093 0 0 3163 0 0 2958 0 0 3164 0 0 1895 0 0 3165 0 0 1382 0 0 3166 0 0 2869 0 0

Hope this helps!


The way forward always starts with a minimal test.

In reply to Re: Trouble iterating through a hash by 1nickt
in thread Trouble iterating through a hash by e9292

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.