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

Hi Monks, My annoying problem is: I am looping through an array of @numbers from 1-50 with some numbers missing. I am comparing these numbers to that of a $counter, if the two numbers are equal, I want to print an image to the screen, if the two numbers are different e.g. $counter = 10, number = 9, i print a different image.

My problem is occuring after a missing number has been found, I am struggling to get the counter to catch up with the number - I have tried +=1 and all sorts but my efforts mean that the wrong image is printed after the missing number (because the right image is only printed when the numbers are equal). How can I change this to still print one of the images after a missing number has been found??

Hopefully some code will explain this: Thanks a lot.

for my $counter ( 1..100 ) { for my $i ( @numbers ) { my $img; print "WELL: $i <P> GOAL: $counter<P>"; if ($i == $counter) { $img = $right[$counter-1]; } else { $img = "error.bmp"; $i -= 1; } $counter++; print qq(<area href="http:///cgi-bin/$img" >); } }

Replies are listed 'Best First'.
Re: Real confusion with loops and counters
by Bilbo (Pilgrim) on May 30, 2003 at 11:53 UTC

    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"; }
•Re: Real confusion with loops and counters
by merlyn (Sage) on May 30, 2003 at 14:16 UTC
Re: Real confusion with loops and counters
by Itatsumaki (Friar) on May 30, 2003 at 14:20 UTC

    If I were you I would reword my question a bit. You aren't comparing two lists of sorted numbers, really, but rather two lists of "things". You want to check everything in list1 to see if it is in list2. If it is, go for a party! If not, go to a different party! (Sorry, early Friday morning sense of humour there). So, you could write it like this:

    my @list1 = (1,3,4,5,6,7); my @list2 = (1..100); foreach my $list1_item (@list1) { my $flag = 0; foreach my $list2_item (@list2) { if ($list1_item == $list2_item) { $flag = 1; last(); } } if ($flag == 0) { print $img1; } else { print $img2; } }

    So basically we just loop over list1, see if it's elements are in list2. If they are, set the flag variable, stop looping and print it. If they aren't, print the alternate image. There are definitely more concise and efficient ways to code that, but this is clear and will work if you move from numbers to strings, objects, etc.

    -Tats