in reply to Re^2: Creating a column of frequency for the unique entries of another column
in thread Creating a column of frequency for the unique entries of another column
When you want to count the number of times that each of an assortment of things appears, that usually means you want to use a hash, with the 'things' as the keys and the count kept as the values. In this case, use the sequences as your hash keys and increment each one's value each time it appears:
# my %freq; #<---- uncomment this somewhere before your loop # through the file to instantiate the hash if ($line=~m/$sequence(.{11})(.{11})$sequence/){ $freq{$1}++; $freq{$2}++; } } # after all lines are read in.... # sort through the sequences, highest frequency to lowest for my $seq (sort { $freq{$b} <=> $freq{$a} } keys %freq){ print "$seq $freq{$seq}\n"; }
|
|---|