in reply to Table manipulation, array or hash?

Think about a data structure called a Hash of Array..
#!/usr/local/bin/perl use strict; use warnings; my %hash; while(<DATA>){ chomp; my ($SNP, $IND, $COL1, $COL2, $COL3)= split(/\s+/); push @{$hash{$IND}}, $COL1, $COL2 if $IND; } use Data::Dumper; print Dumper(\%hash); __DATA__ SNP5 IND1 A5 C5 0.8 SNP2 IND1 A2 C2 0.8 SNP1 IND1 A1 C1 0.8 SNP3 IND1 A3 C3 0.8 SNP4 IND1 A4 C4 0.8 SNP5 IND2 G5 T5 0.8 SNP2 IND2 G2 T2 0.8 SNP1 IND2 G1 T1 0.8 SNP3 IND2 G3 T3 0.8 SNP4 IND2 G4 T4 0.8
check The Data Structure Cookbook.

sorting and printing the outcome is left as an exercise to the OP


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re^2: Table manipulation, array or hash?
by robertkraus (Novice) on Mar 23, 2010 at 11:27 UTC
    Thanks a lot! Looks extremely efficient, good to know that there is a module that takes over some of the work!
      Data::Dumper is a module that dumps snapshots of your variables/values. It's not really doing anything in this case, other than being used for debugging. The work being done is in the loops.