loop won't run, for ( my $i = 10 ; $i == 1 ; etc

J

Justin C

I can't see why this won't run, I've the Learning Perl book here and,
though I'm not using $i as per the example, I've followed it (AFAICT)
exactly. Here's the code:

#!/usr/bin/perl

use warnings ;
use strict ;

my @data = qw/ john paul george ringo / ;

thumbs(@data) ;

sub thumbs {
my $row_count = 5 ;
for ( my $pg_count = 15 ; $pg_count == 0 ; $pg_count-- ) {
print "$pg_count PC" ;
$row_count-- ;
$pg_count-- ;
}
}

This obviously isn't all my code but it's the shortest example that
demonstrates the problem.

I bet it's something really simple, but I can't see it... I'm tensed
and ready for the "stoopid noob" slap :)

What I've done to test this is litter the entire code with print
statements, the sub is called but the 'for' loop just appears to be
skipped.

Perl 5.8.4, Debian amd64 (Sarge, unofficial port).

I'm looking and looking at the loop:

The first part is an assignement, the second part is a test, the third
part is the increment (or whatever). Nope, I cannot see what it is.

Thank you for your time.

Justin.
 
P

Paul Lalli

I can't see why this won't run, I've the Learning Perl book here and,
though I'm not using $i as per the example, I've followed it (AFAICT)
exactly. Here's the code:

for ( my $pg_count = 15 ; $pg_count == 0 ; $pg_count-- ) {
print "$pg_count PC" ;
$row_count-- ;
$pg_count-- ;
}

}This obviously isn't all my code but it's the shortest example that
demonstrates the problem.

I bet it's something really simple, but I can't see it... I'm tensed
and ready for the "stoopid noob" slap :)

Consider yourself slapped. :p

A for loop's three parts are INIT, TEST, INCR. The initialization is
my $pgcount = 15;
That's fine. Then as soon as that initialization is done, the TEST is
checked. If and only if the TEST is true, does the loop execute. Your
test is:
$pgcount == 0
This is obviously false, so the TEST fails and the loop ends. Your
confusion seems to be in thinking that the loop executes *until* the
test is true. Not so - it executes *while* the test is true. Change
your test to:
$pgcount > 0
and all will be well. Except for the fact that you're decrementing
your variable twice - once in the loop header and once in the loop
body. Maybe that's what you meant to do, maybe it's not. That depends
on your program logic.

Paul Lalli
 
C

Cliff Martin

I can't see why this won't run, I've the Learning Perl book here and,
though I'm not using $i as per the example, I've followed it (AFAICT)
exactly. Here's the code:

#!/usr/bin/perl

use warnings ;
use strict ;

my @data = qw/ john paul george ringo / ;

thumbs(@data) ;

sub thumbs {
my $row_count = 5 ;
for ( my $pg_count = 15 ; $pg_count == 0 ; $pg_count-- ) {
print "$pg_count PC" ;
$row_count-- ;
$pg_count-- ;
}

}This obviously isn't all my code but it's the shortest example that
demonstrates the problem.

I bet it's something really simple, but I can't see it... I'm tensed
and ready for the "stoopid noob" slap :)

What I've done to test this is litter the entire code with print
statements, the sub is called but the 'for' loop just appears to be
skipped.

Perl 5.8.4, Debian amd64 (Sarge, unofficial port).

I'm looking and looking at the loop:

The first part is an assignement, the second part is a test, the third
part is the increment (or whatever). Nope, I cannot see what it is.

Thank you for your time.

Justin.

your test in your for loop will always fail. Try:

for ( my $pg_count = 15 ; $pg_count >= 0 ; $pg_count-- ) {

Cliff
 
J

Justin C

Your
confusion seems to be in thinking that the loop executes *until* the
test is true. Not so - it executes *while* the test is true. Change
your test to:
$pgcount > 0

Thank you Paul, and Cliff, for clear, concise answers. You're both,
obviously, spot on. I don't use this type of loop very often (whaddya
mean you can tell?) and I'm sure it'll come back and bite me again next
time.

Feel a bit of a fool!

Anyway, thanks again.

Justin.
 
D

DJ Stunks

Thank you Paul, and Cliff, for clear, concise answers.
I don't use this type of loop very often

Nobody does, because it's very rare you can't use a Perl for:

C:\>perl -e "print qq[$_,] for reverse 0..15"
15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,

-jp
 
A

anno4000

DJ Stunks said:
Thank you Paul, and Cliff, for clear, concise answers.
I don't use this type of loop very often

Nobody does, because it's very rare you can't use a Perl for:

C:\>perl -e "print qq[$_,] for reverse 0..15"
15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,

True, but Justin's original example was essentially

for ( my $i = 5; $i >= 0; -- $i ) {
# ...
-- $i; # extra decrement
# ...
}

Intentionally or not, this is an example where the C-Style loop
is not easily replaced with a Perl style for loop.

for my $i ( reverse 5 .. 0 ) {
# ...
-- $i; # extra decrement
# ...
}

behaves quite differently.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top