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

#!/usr/bin/perl -w use strict; my @a1 = qw(My name is Andrew); my @a2 = qw(My dog named Andrew); my $i; my $a = 0; my $s = 0; my $z = 0; my $aa; my $ss; my $n = "\n"; my $t = "\t"; foreach my $a1 (@a1){ $i++; #print $a1 . "\n"; } First:for($a = 0; $a <= $i; $a++){ Second:for($s = 0; $s <= $i; $s++){ my $d = 0; print $a.$t; print $s.$n; if($a!=$s){ print "Yes.->".$a.$s.$n; last} print "system command \$a $a, then for \$s $s".$n; $d++; } }

What I am trying to do here, use this nested for loop to get numbers that don't match. I need this to be continous, as this is a blueprint for another project. Now it appears that when I do the code, I get this,

0 0 system command $a 0, then for $s 0 0 1 Yes.->01 1 0 Yes.->10 2 0 Yes.->20 3 0 Yes.->30 4 0 Yes.->40
when i need it to do this
system command $a 0, then for $s 0 system command $a 0, then for $s 1 system command $a 0, then for $s 2 system command $a 0, then for $s 3 system command $a 0, then for $s 4 system command $a 1, then for $s 0 system command $a 1, then for $s 1 system command $a 1, then for $s 2 .... and so on and so on.
My problem is that I can't seem to get the script to loop through the for loops after the $_'s go through the boolean statements. It seems the boolean statements cause the for loops to not progress. I need to have the boolean statements to get unequal numbers. This is the best I can explain it. Any tips. Cheers

Replies are listed 'Best First'.
Re: how to repeat a if else statement in a nested for loop
by graff (Chancellor) on Jun 19, 2015 at 01:46 UTC
    You really need to try harder to explain it, because so far, you seem to be contradicting yourself. First you say that you're "trying ... to get numbers that don't match", and then you say "I need it to do this" and you give sample output where it makes no difference whether the numbers match or not. So which is it?

    You might be getting confused by your poor use of indentation, and your irregular use of line breaks vs. spaces between statements. Here's how the nested "for" loops in the OP script look when formatted in the "normal" Perlish way (and with a normal use of "print"):

    for ( $a = 0; $a <= $i; $a++ ) { for ( $s = 0; $s <= $i; $s++ ) { my $d = 0; print "$a\t$s\n"; if ( $a != $s ) { print "Yes.->$a$s\n"; last; } print "system command \$a $a, then for \$s $s".$n; $d++; } }
    Having "last" inside the "if" block means that as soon as $a and $s have different values, you go directly to the next value of $a. When $a is 0, you get two iterations on $s, but after that (with $a > 0) you exit the inner loop immediately (because $s always start at 0, which is always different from $a), so you never reach the "system command" print statement.

    Proper indentation is important, especially when you're already confused about other things.

Re: how to repeat a if else statement in a nested for loop
by AnomalousMonk (Archbishop) on Jun 19, 2015 at 00:55 UTC
    ... when i need it to do this ...

    I'm not really sure where you're going with this, but maybe something like the following can serve as a point of departure:

    c:\@Work\Perl\monks>perl -wMstrict -e "for my $i (0 .. 3) { for my $j (0 .. 3) { print qq{system command \$i $i, then for \$j $j \n}; } } " system command $i 0, then for $j 0 system command $i 0, then for $j 1 system command $i 0, then for $j 2 system command $i 0, then for $j 3 system command $i 1, then for $j 0 system command $i 1, then for $j 1 system command $i 1, then for $j 2 system command $i 1, then for $j 3 system command $i 2, then for $j 0 system command $i 2, then for $j 1 system command $i 2, then for $j 2 system command $i 2, then for $j 3 system command $i 3, then for $j 0 system command $i 3, then for $j 1 system command $i 3, then for $j 2 system command $i 3, then for $j 3

    Update: Then maybe something along the lines of:

    c:\@Work\Perl\monks>perl -wMstrict -e "for my $i (0 .. 3) { INNER: for my $j (0 .. 3) { next INNER if $i == $j; print qq{system command \$i $i, then for \$j $j \n}; } } " system command $i 0, then for $j 1 system command $i 0, then for $j 2 system command $i 0, then for $j 3 system command $i 1, then for $j 0 system command $i 1, then for $j 2 system command $i 1, then for $j 3 system command $i 2, then for $j 0 system command $i 2, then for $j 1 system command $i 2, then for $j 3 system command $i 3, then for $j 0 system command $i 3, then for $j 1 system command $i 3, then for $j 2


    Give a man a fish:  <%-(-(-(-<

Re: how to repeat a if else statement in a nested for loop
by Random_Walk (Prior) on Jun 19, 2015 at 06:32 UTC

    First off, welcome to the Monastery. We hope you enjoy your stay.

    Looking at your post, it is hard to understand what you are trying to achieve. Your specification is not so clear. But lets we what we can do...

    my @a1 = qw(My name is Andrew); my @a2 = qw(My dog named Andrew);

    First off you define two arrays of words, but later only one is used. Perhaps this is a clue...

    foreach my $a1 (@a1){ $i++; #print $a1 . "\n"; }

    Is this here to count the words in @a1 and set $i? If so we can skip it, and go an easier way later on

    First:for($a = 0; $a <= $i; $a++){ Second:for($s = 0; $s <= $i; $s++){ my $d = 0; print $a.$t; print $s.$n; if($a!=$s){ print "Yes.->".$a.$s.$n; last} print "system command \$a $a, then for \$s $s".$n; $d++; } }

    OK, so we have two loops, counting from 0 to $i. We have a $d in there that I don't think you use again. Then we print out a line, when the iterators are not equal. All very mysterious.

    Wild Guess

    Perhaps you are trying to compare the words in the lists? I see two word lists, and some initialisation based on the length of one of them. We may not even need nested loops. If that's what you are after, how about something like this...

    use strict; use warnings; my @a1 = qw(My name is Andrew); my @a2 = qw(My dog named Andrew); my $i = 0; while ($a1[$i] and $a2[$i]) { # make sure something is in both arrays if ($a1[$i] ne $a2[$i]) { print "system command \$a1 $a1[$i], then for \$a2 $a2[$i]\n"; } else { print "# \$a1 $a1[$i], and \$a2 $a2[$i] are the same\n"; } $i++; }

    Like I said, a wild guess. Please do update your specification, so we can help a little more

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      Another approach to a wild guess:

      c:\@Work\Perl\monks>perl -wMstrict -e "my @a1 = qw(My name is Andrew); my @a2 = qw(My dog named Andrew); ;; for my $w1 (@a1) { INNER: for my $w2 (@a2) { next INNER if $w1 eq $w2; print qq{system command a1 $w1 and a2 $w2 \n}; } } " system command a1 My and a2 dog system command a1 My and a2 named system command a1 My and a2 Andrew system command a1 name and a2 My system command a1 name and a2 dog system command a1 name and a2 named system command a1 name and a2 Andrew system command a1 is and a2 My system command a1 is and a2 dog system command a1 is and a2 named system command a1 is and a2 Andrew system command a1 Andrew and a2 My system command a1 Andrew and a2 dog system command a1 Andrew and a2 named
      Others have commented on the difference between last (found in the OPed code example), which completely exits its associated loop, and next, which terminates the current iteration of its loop and begins the next one (if there is one).


      Give a man a fish:  <%-(-(-(-<

Re: how to repeat a if else statement in a nested for loop
by Laurent_R (Canon) on Jun 19, 2015 at 06:35 UTC
    Your two nested loops could boild down to:
    for my $a (0..$i) { for my $s (0..$s) { if ($a != $s) { # do something } } }
    But it really makes very little sense.