-----Original Message-----
From: Randal L. Schwartz [mailto:[email protected]] 
Sent: Monday, May 25, 2009 10:10 PM
To: [email protected]
Subject: Re: Difference between for & foreach loop

>>>>> ""sanket" == "sanket vaidya" <[email protected]> writes:

"sanket> What is the difference between the 'for' & 'foreach' loops? I know
that they
"sanket> can be used interchangeably then what is the purpose of keeping
them
"sanket> separate? Can anyone suggest me a good url which can tell the
difference
"sanket> between two?

*************************************************************************
Back in the early days of Perl, Larry Wall decided to borrow from both C
(the
"for" loop) and Csh (the "foreach" loop) to provide a means of a computed
iterator or a simple list enumeration.

Apparently, he noticed that Perl could always distinguish (by the contents
of
the parentheses part of the statement) whether a for-loop or a foreach-loop
was needed, Larry decided to make the keywords interchangable, simply
because they were both "iterators" and the more common of the two loops
(the foreach loop) could be spelled with 4 fewer characters.

In the early documentation (the first Camel which I wrote with Larry, and
the
first Llama), we were very careful to describe the aliasing of the keyword,
but *also* very careful to always use the proper keyword for the loop type
in
every example, just to make sure it was clear to the early Perl users that
these really are two distinct types of loop.

Later documentation that has come along (*not* written by me or Larry) has
more sloppily merged the two, and made it more confusing, I think.  I can
predict followups to my message here that argue the opposite of what i'm
saying.  Rest assured, *they* were *not* there in the beginning, nor have
had
as much time sitting across the dinner table with Larry himself as I have
had,
so I speak from authority on this matter.

To summarize:  the "for" loop is from C, and looks like:

  for (initializer; test; incrementor) { body }

The "foreach" loop is from csh, and looks like:

  foreach $optional_iterator (list) { body }

However, you can spell "for" as "foreach" and vice versa, and Perl
won't care... it'll correct for your misspelling. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

***********************************************************************

Thanks Randal. That explanation was good one. 

So to conclude
'for' & 'foreach' are the two different ways to write same thing & there is
no particular advantage of using one over other.

Group, correct me if I am wrong.

Regards,
Sanket Vaidya



_____________________________________________________________________ 

This e-mail message may contain proprietary, confidential or legally privileged 
information for the sole use of the person or entity to whom this message was 
originally addressed. Any review, e-transmission dissemination or other use of 
or taking of any action in reliance upon this information by persons or 
entities other than the intended recipient is prohibited. If you have received 
this e-mail in error kindly delete this e-mail from your records. If it appears 
that this mail has been forwarded to you without proper authority, please 
notify us immediately at [email protected] and delete this mail.
_____________________________________________________________________

-- 
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to