I am new to Perl. I have created an array of the lines in my file (split on the white space. I want to exclude the query line. I am interested in element 2 (which is a string ie Streptococcus). I want to create a hash where a new key is created each time a novel string appears and the value will be 1. Each time it sees a string that has already been seen, I want to add 1 to the value of that key. My code returns a new key for every line with the value 2...What am I doing wrong? Thanks
#Example data: #Query= 16sV5:44:FC62N5DAAXX:7:1:18808:1248 1:N:0: #1111599 AB563244.1 Streptococcus macedonicus str. W28 #1111215 AB563262.1 Streptococcus equinus str. MTS6 #repeats thousands of times... while (my $line = <IN>) { chomp $line; if ($line =~ /Query=/) { } else{ my @hit = split (/\s/, $line); my %genus = ($hit[2] , 1) ; if (exists ( $genus {$hit[2]})) { $genus{$hit[2]}++; } else{ $genus{$hit[2]} = 1; } my @keys = keys %genus; my @values = values %genus; print "@keys\n@values\n"; } next; }
In reply to Creating hash: Key is unique string in array element, value is number of times it appears by putmant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |