I am trying to fix an error I get when working on an array. The code below has an array with (8) 1s, (6) 2s, (2) 3s, (5) 4s, and (4) 5s. I need to figure this out in order for me to proceed to the next part of my program. If you notice the output below the code I am printing (9) 1s when it should only be 8. Does anyone in the Monastery have any thoughts?
@ID = (1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,4,4,4,4,4,5,5,5,5); $evr_id = 0; $count = 1; $flag = 0; $processed = 1; print "\n"; foreach $a (@ID) { if($flag == 0) { $evr_id = $a; print "$processed) $evr_id-$count\n"; $flag = 1; $count++; }elsif($evr_id != $a) { print "$processed) $evr_id-$count\n\n"; $count = 1; $evr_id = $a; $flag = 0; }else{ print "$processed) $a-$count\n"; $count++; } if(scalar(@ID) == $processed) { print $processed + 1 . ") $a-$count\n"; } $processed++; } OUTPUT: 1) 1-1 2) 1-2 3) 1-3 4) 1-4 5) 1-5 6) 1-6 7) 1-7 8) 1-8 9) 1-9 10) 2-1 11) 2-2 12) 2-3 13) 2-4 14) 2-5 15) 2-6 16) 3-1 17) 3-2 18) 4-1 19) 4-2 20) 4-3 21) 4-4 22) 4-5 23) 5-1 24) 5-2 25) 5-3 26) 5-4

In reply to Problems with looping through an array by tcox

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.