- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Another perl question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 10:30 AM
12-04-2003 10:30 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 10:45 AM
12-04-2003 10:45 AM
Re: Another perl question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 10:47 AM
12-04-2003 10:47 AM
Solution>(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 10:49 AM
12-04-2003 10:49 AM
Re: Another perl question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:05 AM
12-04-2003 11:05 AM
Re: Another perl question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:09 AM
12-04-2003 11:09 AM
Re: Another perl question
so the inner loop is incremented whilst the outer loop stays at a fixed value until the product is met ?
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:15 AM
12-04-2003 11:15 AM
Re: Another perl question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:21 AM
12-04-2003 11:21 AM
Re: Another perl question
I may embarass myself here
How have you got the programme to run ?
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:23 AM
12-04-2003 11:23 AM
Re: Another perl question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:25 AM
12-04-2003 11:25 AM
Re: Another perl question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:26 AM
12-04-2003 11:26 AM
Re: Another perl question
tehe
Thanks everyone
more questions to come , no doubt
steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:31 AM
12-04-2003 11:31 AM
Re: Another perl question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 11:38 AM
12-04-2003 11:38 AM
Re: Another perl question
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 06:29 PM
12-04-2003 06:29 PM
Re: Another perl question
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