Quick Search for:  in language:    
example,basic,Perl,loop
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 74,273. lines
 Jobs: 24. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Perl.
Click here to see a screenshot of this code!CGIScripter
By David Simpson on 11/24

(Screen Shot)

Calender
By Jeff Mills on 11/20


quikpoll
By Jeff Mills on 11/20


Encrypt Password
By Jeff Mills on 11/20


Rock, Paper, Scissors w/ GUI
By Kurt Rudolph on 11/19


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Basic Perl Loop Tutorial

Print
Email
 

Submitted on: 1/31/2001 4:50:02 PM
By: Sirrus 
Level: Beginner
User Rating: By 10 Users
Compatibility:3.0 (all versions)

Users have accessed this article 17773 times.
 
(About the author)
 
     This is an example of a basic Perl loop.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
for($x=0;$x<=100;$x++) {

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
1/31/2001 6:16:15 PM:Brandon McPherson
Where is the rest of it?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/31/2001 6:40:20 PM:Sirrus
That is all of it, a loop is just one line of code
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/31/2001 10:19:30 PM:Brandon McPherson
There should also be a closing }, no?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/1/2001 5:43:03 AM:Pablo Robert
I think you hit the enter before the time!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/2/2001 7:11:05 AM:James Lamb
Are some of you looking for something like this for($x=0;$x<=100;$x++) { print
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/2/2001 7:13:27 AM:James Lamb
Let me try this again for($x=0;$x<=100;$x++) { print "Hello!!!";}
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/9/2001 12:57:04 PM:PaC
Perl has multiple loops. while() for() goto label; recursive subs can even be loops.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/21/2001 9:20:32 AM:Travis
aren't you always going to be stuck in this loop because you are constantly reseting the value of $x to 0 ??
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/2/2001 9:45:09 AM:Brandon McPherson
No Travis, because you're saying that ("X will start at 0, do the task and add one to X. Keep doing that until X=99 or 100")
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/21/2001 12:14:12 PM:Bruno Ribeiro
ROFLMAO @ your article... lol lol lol
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/21/2001 6:17:19 PM:Robin North
The construct is not complete without the closing brace, and Brandon, it finishes when $x is greater than 100.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/22/2001 9:34:44 AM:Joseph Hunter
actually.. < means less than.. x starts at 0 so.. that loop wont even run.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/22/2001 4:03:37 PM:Bruno Ribeiro
LOL @ your comment Joseph! the loop will run! have u ever coded in perl? if not just run this perl progie: #!/usr/bin/perl for($x=0;$x<=1 00;$x++){ print "\n$x"; } by doing this u will c that the loop stops when $x = 100, it is a shame having ppl who don't even know what they r saying posting comments, plz go learn b4 saying anything (the code will print 101 lines each line contains a # from 0 to 100). Bleh. Laterz
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/23/2001 6:22:59 AM:Robin North
Technically Bruno the loop stops when $x equals 101. The post incremental operator ($x++) increments $x after execution of code contained within loop and then the loop test ($x<=100) returns false i.e $x is NOT less than or equal to 100, thus ending the looping process. I thought this was a forum for helping ppl not flaming them - I think you need to read what YOU wrote as YOUR loop test! HTH
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/23/2001 12:09:43 PM:yeah sorry
I just get pissed when i c ppl who don't even know what they r saying posting comments saying something is wrong, i wrote this code in the momment i saw the post about that guy answering to yer other post. I didn't even test it it is just obvious what he code would do. Laterz
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/29/2001 3:52:03 AM:Iwan
LOL, this stuff is funny, i thought this was a place for coders, but i see also humorists are welcome. By the way, $x starts at 0, then it checks if $x is below or equal to 100, then adds 1. Since it is is a for loop it won't continue executing the code inside the loop when $x is greater then 100, it will however add 1 to $x AFTER the the max is reached, so it will come to a total of 101, not loops, but $x. if you dont understand, check it with the following code: #! /usr/bin/perl -w for ($x = 0; $x <= 100; $x++ ) { print
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/29/2001 3:54:02 AM:Iwan
the code from the last post was messed up, here it comes again: for ($x = 0; $x <= 100; $x++ ) { print "$x\n"; } print "$x\n";
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/16/2001 1:25:32 PM:James
Yah, this code is ignorant. Anbody who knows Perl knows how to do a simple loop such as this one...ugh! Post something worth reading and viewing ;-) Oh, btw, check out my site <a href="http://www.syn2k.net/">Synthesize VB</a>. We have alot of VB stuff but the whole site is based off Perl so if you wanna see a website with some Perl action, then scope this site. Thanx
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/21/2001 1:57:24 AM:Synthesize
Hey James! I didn't know that you were a member here! Please, everyone, check out our site!!! It is Perl-based, like James said. I did most of it, but I am letting James do a bit here and there. It is a good start, but we need to get all of the pages up! Bye!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/1/2002 6:04:04 PM:Hawk
I think what confuses most programmers is the fact it says ;$x<=100; when you are thinking in your head "when $x>=100 then stop". The logic of the "for" loop is actually "so long as $x<=100, then keep going". The fact the author forget his ending } is irrelevent since you know to do this anyway.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/8/2002 3:17:37 PM:mike
Hi, How do we break in the middle of the loop? a
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/7/2003 5:32:12 PM:jbrahy
ok, let's really screw up your paradigm... Larry's favorite quote is,
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.