in reply to Nested Conditional Statement Issue

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

Replies are listed 'Best First'.
Re (tilly) 2: Nested Conditional Statement Issue
by tilly (Archbishop) on May 07, 2001 at 03:01 UTC
    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