putmant has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creating hash: Key is unique string in array element, value is number of times it appears
by toolic (Bishop) on Oct 25, 2011 at 23:51 UTC | |
by putmant (Initiate) on Oct 26, 2011 at 17:26 UTC | |
|
Re: Creating hash: Key is unique string in array element, value is number of times it appears
by spazm (Monk) on Oct 26, 2011 at 01:14 UTC | |
by johngg (Canon) on Oct 26, 2011 at 11:06 UTC | |
|
Re: Creating hash: Key is unique string in array element, value is number of times it appears
by Khen1950fx (Canon) on Oct 26, 2011 at 02:54 UTC |