Operating System - HP-UX
1831294 Members
3297 Online
110022 Solutions
New Discussion

Re: Another perl question

 
SOLVED
Go to solution
steven Burgess_2
Honored Contributor

Another perl question

Hi everyone

I understand that the 'last' statement will exit the loop if the defined condition is met.

If you have multiple nested loops then 'last' will exit the innermost loop, again when the defined condition is met

Some of you may read the next statement and understand exactly what is meant. I however am struggling and would be grateful if anyone could expand on the authors comments. I'm going to type it, exactly how it's written. I have put my queries within the paragraph in brackets

The set of nested loops in fig 3.2 finds all the whole numbers less than 100 whose products are 140-2 and 70,4 and 35,(it only prints if ==140, what does he mean ?) and so on - rather inefficently. The point to note here is the 'last' statement. When the product is found (140), the result is printed and the inner loop is exited (understood). The outer loop continues executing and reruns the inner loop (does that mean the next check will be the previous value of $j and the next incremented value of $i)

fig 3.2

/usr/bin/perl -w

for ($i=0; $i<100; $i++ ) {
for ($j=0; $j<100; $j++) {
if ( $i * $j == 140) {
print "The Product of $i and $j is 140\n";
last ;
}
}
}

Thanks in advance

Steve
take your time and think things through
13 REPLIES 13
James A. Donovan
Honored Contributor

Re: Another perl question

When i=1 and j=140, the print statment will execute, and the last statement will exit the inner loop. i will be incremented to 2 and then the inner loop will begin again with j=0. When j=70, the print and last statements will again be executed, sending the process back to the outer loop to increment i to 3. etc., etc...
Remember, wherever you go, there you are...
Hein van den Heuvel
Honored Contributor
Solution

Re: Another perl question

> products are 140-2 and 70,4 and 35,
>(it only prints if ==140, what does he mean ?)

Yes, and the first solution found is 2*70, the next 4*35.
You have run the program right?! (See below)

> The outer loop continues executing and reruns the inner loop

Let's say i is 2 in the outerloop.
The innerloop increments J from 0 to 70 and then it recognizes a solution. So it prints and issues last. The inner (j) loop is now done. j does not become 71 this time around. The outerloop gets control, finds no more statements and goes to loop control to increment i to 3. As first (and only) statement in the new outer loop iteration it restarts the inner loop incrementing j from 0 to 100... without ever finding a solution. It drops through to the outer, i is made 4, the inner starts j again at 0 and finds the next solution when j becomes 35... and so on.


Cheers,
Hein.

The Product of 2 and 70 is 140
The Product of 4 and 35 is 140
The Product of 5 and 28 is 140
The Product of 7 and 20 is 140
The Product of 10 and 14 is 140
The Product of 14 and 10 is 140
The Product of 20 and 7 is 140
The Product of 28 and 5 is 140
The Product of 35 and 4 is 140
The Product of 70 and 2 is 140



James A. Donovan
Honored Contributor

Re: Another perl question

ooops...neither i nor j will ever be allowed to reach the value of 140. So, while the logic of my last statement is still true, the actual first execution of the print and last statements will not occur until {i,j} = {2,70}
Remember, wherever you go, there you are...
steven Burgess_2
Honored Contributor

Re: Another perl question

Hi, thanks for the replies

reading now, still struggling!

I tried to run the loop to see exactly what it's doing but it didn't do anything!! . I have it exactly as above

How does the loop control the increment of each variable ?

Thanks again

Steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: Another perl question

nearly there

so the inner loop is incremented whilst the outer loop stays at a fixed value until the product is met ?

Steve
take your time and think things through
James A. Donovan
Honored Contributor

Re: Another perl question

Precisely! You got it.
Remember, wherever you go, there you are...
steven Burgess_2
Honored Contributor

Re: Another perl question

Hein

I may embarass myself here

How have you got the programme to run ?

Steve
take your time and think things through
James A. Donovan
Honored Contributor

Re: Another perl question

A good analogy just occurred to me...think of the loops as if they represented the hour and minute hands of a clock. The outer loop is equivalent to the hour hand, while the inner loop represents the minute hand. The minute hand (inner loop) will be incremented 60 times before the hour hand (outer loop) is incremented once. Once the hour hand is incremented, the minute hand is "reset" to its starting position and must increment another 60 times before the hour hand is once again incremented....
Remember, wherever you go, there you are...
James A. Donovan
Honored Contributor

Re: Another perl question

create a file called test.pl....

insert the following...

#!/usr/bin/perl -w

for ($i=0; $i<100; $i++ ) {
for ($j=0; $j<100; $j++) {
if ( $i * $j == 140) {
print "The Product of $i and $j is 140\n";
last ;
}
}
}


save it...and then...

$ perl test.pl
The Product of 2 and 70 is 140
The Product of 4 and 35 is 140
The Product of 5 and 28 is 140
The Product of 7 and 20 is 140
The Product of 10 and 14 is 140
The Product of 14 and 10 is 140
The Product of 20 and 7 is 140
The Product of 28 and 5 is 140
The Product of 35 and 4 is 140
The Product of 70 and 2 is 140

$ perl test.pl

Remember, wherever you go, there you are...
steven Burgess_2
Honored Contributor

Re: Another perl question

thats a pretty good analogy, it's a pity the text doesn't explain exactly how the loops work and just presume you know, or is that just because i'm a little slow ?

tehe

Thanks everyone

more questions to come , no doubt

steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: Another perl question

i'm not going to tell you where the deliberate mistake was

it didn't error though when i did a ./prog.pl but only when i did perl prog.pl which gave me a clue

Cheers

Steve

take your time and think things through
steven Burgess_2
Honored Contributor

Re: Another perl question

If i'd have got the code to run, i may have saved a whole load of questions

here's how i got it to explain itself

#!/usr/bin/perl -w

for ($i=0; $i<100; $i++ ) {
print "This is the outerloop and has value $i\n";
for ($j=0; $j<100; $j++) {
print "This is the inner loop and has value $j\n";
if ( $i * $j == 140) {
print "The Product of $i and $j is 140\n";
last ;
}
}
}


Thanks again

Steve
take your time and think things through
H.Merijn Brand (procura
Honored Contributor

Re: Another perl question

For something completely different, what you are doing is programming C in perl, where perl has much more beautiful ways to express such loops:

foreach my $i (0..140) {
foreach my $j (0..140) {
$i * $j == 140 or next;
print "The product of $i and $j is 140\n;
last;
}
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn