CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
need help with next statement

 



y2k
New User

Jan 17, 2003, 5:36 AM

Post #1 of 3 (397 views)
need help with next statement Can't Post

I'm a newbie to perl so go easy!!

What does the next statement do? The following code is ain a book I'm reading right now:

Code
for($i=0, $i <100, $i++) 
{
next if(not $i % 2);
print "An odd number is $i";
}

The paragraph underneath says that this will print all the even numbers between 0 & 98 - but the print statement says otherwise!! So I'm confused. I know what the for statement is doing. And the if is saying if the modulus of $i divided by 2 is 0 return true else, return false (values are reversed because of the "not"). However, is the next saying "if the if statement evaluates to tru, then continue with the remainder of the statements withint the {}" or is it saying "if the if statement is true, then go onto the next iteration of the for loop"?

Hope this doesn't sound confusing?


davorg
Thaumaturge / Moderator

Jan 17, 2003, 8:32 AM

Post #2 of 3 (390 views)
Re: [y2k] need help with next statement [In reply to] Can't Post

Sounds like you've got a pretty dodgy book there. What is it called? The books that are recommended by the Perl community are listed in the FAQ.

To answer your question, "next" abandons the current iteration of the loop and goes immediately to the next execution of the loop control statement.

Your code prints the _odd_ numbers to 99. Or rather, it would if it didn't have a bug in it (the three sections of the "for" statement should be separated by semicolons, not commas).

Other than that, it's a really piece of code. You'd be better off writing it as:

Code
for (0 .. 99) { 
print "$i is odd\n" if $_ % 2;
}


I think you really should tell us what the book is so we can advise people to avoid it :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


y2k
New User

Jan 20, 2003, 2:41 AM

Post #3 of 3 (379 views)
Re: [davorg] need help with next statement [In reply to] Can't Post

Hi Davorg,

Thanks for your response. Actually, the bug isn't in the book (the one in the for loop) - that's my faultBlush I'm just so used to writing javascript that I automatically put in commas instead of semi-colons - I'm just used to using semi-colons to end a statement!! While your code does work better, the reason why it was written this way was to "explain" the next statement - except that the author confused everybody by saying one thing in the code and a different thing in the explanation!!

The book is Sams Teach Yourself Perl in 24 hours. I have to say. I'm basicly learning perl so that I can write CGI scripts, so I also got Sams Teach Yourself CGI in 24 hours (got the two as a set from amazon.com). I thought I should have a go at reading [most of] the perl book before I moved onto the CGI.

I had a look at the FAQ you referred to, looks like the llama book would be my best option? Well, I'll read some more of the one I have and see how I get onWink

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives