On Thu, Apr 3, 2008 at 8:19 PM, Rob Dixon <[EMAIL PROTECTED]> wrote: > Chas. Owens wrote: > > On Thu, Apr 3, 2008 at 7:29 PM, Ryan <[EMAIL PROTECTED]> wrote: > >> I know they are both the same, I just want to know why we have both. > > snip > > > > Because originally they meant different things. The for loop was a > > c-style loop and the foreach loop was an iterator. Eventually it was > > realized that the iterator style was a better loop and typing foreach > > every time was annoying, so they made for able to do both types of > > loops. In order to keep as much code running as possible the foreach > > loop was kept. Just ignore foreach. > > Hmm. IYHO. It sounds as though you would like to contract all of the > Perl language words into single characters if possible to save typing. > > Although 'foreach' is more useful, I think it's it's far more likely > that someone realized that the two could be distinguished by context and > needn't have different symbols, so the two were made equivalent. snip
No, not the entire language, just those parts that are used most often, like say regexes. It is called Huffman encoding* and it is a large factor in the design of the Perl language. Things you do often should be short and sweet. snip > I preserve 'foreach' to iterate over a list, and use 'for' for C-style > for loops and single elements, like: > for ($string) { > s/^\s+//; > s/\s+$//; > } > > My rule: use the one that best describes in English the function it's > performing. snip In that case, you should be using given () instead of for (), at least in Perl 5.10 and above. * Note, this is not real Huffman encoding, just Larry Wall's version of it. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/