$|=1; # So it works in the IDE
use autodie; # checks file opening for us...

# open files for reading and writing
open FILEIN, "< map.sam";
open FILEOUT, "> tcount.txt";

while(defined($line=<FILEIN>)){ # loop over file line by line
  @a = split /\t/,$line; # split the tab-separated line
  $count{$a[2]}++; # create a hash %count with the key being the FBtr
}                  # each line with that Fbtr will add 1 to the value

@k = sort keys %count; # get all keys from the hash, sort them
foreach $e (@k){ # loop over the keys (the FBtr numbers) and print them along with their values
  print FILEOUT "$e\t$count{$e}\n";
}

close FILEIN;  # always good to close filehandles
close FILEOUT;
