one would do a foreach (<INPUT>){}

No one wouldn't. One might do while (<$inFile>) {...} however. Perl for loops like to work with lists of things and will generally create a list (except in a few special cases) which in the code you suggested would slurp the entire file into memory - something that should generally be avoided.

That aside, I find your sample code very 'busy' with repeated code and needless (and poorly named) variables. Contrast it with the following:

my %dataHash; foreach (@lines) { my ($key1, $key2, $val1, $val2, $rest) = split(/\s+/, $_, 5); my $combinedValue = "$val1,$val2"; $key1 =~ /SNP(\d+)/; $dataHash{$key2}{$1} = $combinedValue; } foreach my $key (sort keys %dataHash) { my %tempHash2 = %{$dataHash{$key}}; print $key; printf(" %2s %2s", split(',', $tempHash2{$_})) for sort keys %temp +Hash2; print "\n"; }

In a teaching context it is desirable to present the cleanest code you can and to demonstrate best practises. Worthwhile exercises for the reader generally entail extending the code in various ways - not in trying to compensate for the sample's deficiencies.


True laziness is hard work

In reply to Re^2: Table manipulation, array or hash? by GrandFather
in thread Table manipulation, array or hash? by robertkraus

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.