Changing the values of $i and $counter within the loop does not look like a good idea to me.

If I've understood you correctly you want to loop through values 1 - 100, checking whether each value is in @numbers. If it is then you want to display an image corresponding to that number, and if it is not then you display an error image.

If this is what you want then you don't need both loops if @numbers is sorted in ascending numerical order:

#!/usr/bin/perl -w use strict; my @numbers = (1, 2, 5, 7, 8, 9); my $pos = 0; for my $counter (1..10) { # Is this value of $counter the next value in @numbers? # If it is then display image number $counter and move onto the # next value in @numbers. # If it is not then display the error image and carry on looking # for the same value in @numbers. if (defined $numbers[$pos] and $numbers[$pos] == $counter) { print "Use image: $counter \n"; $pos++; } else { print "Use error image\n"; } }

Update: I think this is what you were trying to write. This does not depend on the values in @numbers being sorted:

#!/usr/bin/perl -w use strict; my @numbers = (1, 2, 5, 7, 8, 9); for my $counter ( 1..10 ) { my $img; # Check whether this value of counter is in @numbers for my $i ( @numbers ) { if ($i == $counter) { $img = "image" . ($counter-1); last; } } # If $img is still undefined then this value is missing if (!defined $img) {$img = "error.bmp"} print "$counter $img\n"; }

In reply to Re: Real confusion with loops and counters by Bilbo
in thread Real confusion with loops and counters by Anonymous Monk

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.