http://www.perlmonks.org?node_id=356853

tcox has asked for the wisdom of the Perl Monks concerning the following question:

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