in reply to Re: Table manipulation, array or hash?
in thread Table manipulation, array or hash?
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.
|
|---|