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

When I place an if statement with in any type of loop it appears as though that if statement will only evaluate once regardless of how many loop iterations. How would I be able to loop multiple times, evaluate some condition multiple times, and execute code multiple times based on the condition? Here is an example of what I'm trying now (this guy has one nest loop and if statement. It appears once the if statement is true Perl won't evaluate it again:
foreach(@class) { ($id, $name, $credits) = split(/,/, $_); $x=0; while ($clen > $x) { if ($course[$x] eq $id) { $z++; } $x++; } }

Replies are listed 'Best First'.
Re: Nested Conditional Statement Issue
by mincus (Chaplain) on May 07, 2001 at 02:04 UTC
    Looking at that code, it doesnt appear that there is anything wrong with it on the surface..

    I would suggest that you show us the data comming in, and comming out...

    you might also want to throw some print statements in there which always help to find the problem.. ie-
    foreach(@class) { ($id, $name, $credits) = split(/,/, $_); $x=0; while ($clen > $x) { print "COURSE: ->$course[$x]<- IE: ->$id<-$/"; if ($course[$x] eq $id) { $z++; } $x++; } }


    in there so you can verify that your data is really what you think it is..

    Good Luck!

    UPDATE: chromatic made the excellent suggestion that I add something around to vars Im printing to check for white space, YATS (yet another time saver)


    .mincus
    telnet://bbs.mincus.com

Re: Nested Conditional Statement Issue
by srawls (Friar) on May 07, 2001 at 02:39 UTC
    Could you supply the code where you set $clen, @class, and @course? In the meantime, all I can do is offer this revision. It is the same functionally, but it looks nicer to my eyes. (that, of course, does not mean you have to use it, it's just a suggestion)
    for (@class) { ($id, $name, $credits) = split(/,/, $_); for($x=0; $clen > $x; $x++) { $z++ if $course[$x] eq $id; } }


    The 15 year old, freshman programmer,
    Stephen Rawls
      Two tips. First of all use strict. It catches a lot of simple typos (like the one that I suspect caused this question). Secondly in Perl if you see the chance, avoid C-style for loops for Perlish foreach loops whenever you can. Positional logic is notoriously easy to mess up with off by one, etc. For instance supposing that (base on another educated guess) that $cleng is the length of the array @course, this could be rewritten:
      for (@class) { my ($id, $name, $credits) = split(/,/, $_); for my $course (@course) { $z++ if $course eq $id; } }
      If that guess is wrong you could always just loop over the range 0..$cleng-1, or else 0..$cleng. (Although almost certainly my guess is right and you just want to loop over the @course array.)

      And another tip to the original questioner. If you find yourself scanning arrays like this, that is usually a sign that you wanted to use a far more efficient hash lookup instead.

        I'm going to try the use of a hash here if I can...that might just do the trick. Thanks