Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2023-02-06 02:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (33 votes). Check out past polls.

    Notices?