Important alert: (current site time 7/15/2013 8:31:55 PM EDT)
 

article

Basic Perl Loop Tutorial

Email
Submitted on: 1/31/2001 4:50:02 PM
By: Sirrus 
Level: Beginner
User Rating: By 12 Users
Compatibility: 3.0 (all versions)
Views: 65575
(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 tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

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 PMBrandon McPherson

Where is the rest of it?
(If this comment was disrespectful, please report it.)

 
1/31/2001 6:40:20 PMSirrus

That is all of it, a loop is just one line of code
(If this comment was disrespectful, please report it.)

 
1/31/2001 10:19:30 PMBrandon McPherson

There should also be a closing }, no?
(If this comment was disrespectful, please report it.)

 
2/1/2001 5:43:03 AMPablo Robert

I think you hit the enter before the time!
(If this comment was disrespectful, please report it.)

 
2/2/2001 7:11:05 AMJames Lamb

Are some of you looking for something like this
for($x=0;$x<=100;$x++) {
print
(If this comment was disrespectful, please report it.)

 
2/2/2001 7:13:27 AMJames Lamb

Let me try this again for($x=0;$x<=100;$x++) { print "Hello!!!";}
(If this comment was disrespectful, please report it.)

 
2/9/2001 12:57:04 PMPaC

Perl has multiple loops.

while()
for()
goto label;
recursive subs can even be loops.
(If this comment was disrespectful, please report it.)

 
2/21/2001 9:20:32 AMTravis

aren't you always going to be stuck in this loop because you are constantly reseting the value of $x to 0 ??
(If this comment was disrespectful, please report it.)

 
3/2/2001 9:45:09 AMBrandon 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")
(If this comment was disrespectful, please report it.)

 
3/21/2001 12:14:12 PMBruno Ribeiro

ROFLMAO @ your article... lol lol lol
(If this comment was disrespectful, please report it.)

 
3/21/2001 6:17:19 PMRobin North

The construct is not complete without the closing brace, and Brandon, it finishes when $x is greater than 100.
(If this comment was disrespectful, please report it.)

 
3/22/2001 9:34:44 AMJoseph Hunter

actually.. < means less than.. x starts at 0 so.. that loop wont even run.
(If this comment was disrespectful, please report it.)

 
3/22/2001 4:03:37 PMBruno 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<=100;$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
(If this comment was disrespectful, please report it.)

 
3/23/2001 6:22:59 AMRobin 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
(If this comment was disrespectful, please report it.)

 
3/23/2001 12:09:43 PMyeah 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
(If this comment was disrespectful, please report it.)

 
3/29/2001 3:52:03 AMIwan

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
(If this comment was disrespectful, please report it.)

 
3/29/2001 3:54:02 AMIwan

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";
(If this comment was disrespectful, please report it.)

 
4/16/2001 1:25:32 PMJames

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 Synthesize VB. 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
(If this comment was disrespectful, please report it.)

 
4/21/2001 1:57:24 AMSynthesize

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!
(If this comment was disrespectful, please report it.)

 
2/1/2002 6:04:04 PMHawk

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.
(If this comment was disrespectful, please report it.)

 
4/8/2002 3:17:37 PMmike

Hi, How do we break in the middle of the loop? a
(If this comment was disrespectful, please report it.)

 
10/7/2003 5:32:12 PMjbrahy

ok, let's really screw up your paradigm... Larry's favorite quote is,
(If this comment was disrespectful, please report it.)

 
4/15/2005 12:35:57 AMAhmed Wageeh El-Hamouly

i thought i was gonna find a big tutorial... i found an incomplete for loop :)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.