Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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 ( [id://1131105]=note: print w/replies, xml ) Need Help??


in reply to how to repeat a if else statement in a nested for loop

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!

Replies are listed 'Best First'.
Re^2: how to repeat a if else statement in a nested for loop
by AnomalousMonk (Archbishop) on Jun 19, 2015 at 16:11 UTC

    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:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1131105]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (1)
As of 2024-04-25 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found