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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.