Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

RE: Buzzcutbuddha (C Style For and Foreach The Same): Finding the length of an array of arrays

by buzzcutbuddha (Chaplain)
on Aug 18, 2000 at 16:10 UTC ( [id://28468]=note: print w/replies, xml ) Need Help??


in reply to RE (tilly) 2: Finding the length of an array of arrays
in thread Finding the length of an array of arrays

Actually, according to Merlyn, at this node for and foreach are the same and have been since the beginning of Perl. That was all cleared up in for vs foreach.

Minor point of correction... :)
  • Comment on RE: Buzzcutbuddha (C Style For and Foreach The Same): Finding the length of an array of arrays

Replies are listed 'Best First'.
RE (tilly) 2: C Style For and Foreach The Same
by tilly (Archbishop) on Aug 18, 2000 at 17:12 UTC
    No correction in fact. I knew they were aliases all along. But I find it clearer to indicate my thoughts by saying "for" for the classic C loop and "foreach" for iterating over a list. Particularly when I am trying to tell C programmers why they should use foreach style loops rather than for loops. ;-)
      I was correcting you because you said that foreach is faster, etc. I think it would have been better to say that foreach is Perl Style and easier to understand, and better to use for those reasons...

      just my $0.02
        Read what I wrote exactly as I wrote it the first time.

        I said that foreach loops are faster than classic C-style loops. Yes, you can write a classic C-style loop using foreach. But nobody thinks that way.

        And indeed iterating directly over a list is *significantly* faster, less error prone, etc, than doing an explicit loop over the indices, looking up array values inside the loop. That is not disinformation, that is simple fact and I have found that that factual information carries a lot more weight with C programmers starting out with Perl than saying, "Well the standard Perl idiom is..."

        Have you ever said, "This is Perl Style and easier to understand." and been told, "Well I am a C programmer, I have been using for loops for 15 years and they are easier for me to understand because I am used to them."? You won't win that argument...

      Why confuse them though? I would think that telling them up front that they are the same would be easier for them in the long run. Bad enough that some perl programmers still think that they are somehow different. I never use "foreach" myself, but that's because I am exercising the first Perl Virtue. :)

        What makes you think I confuse them?

        I show them the idiom as I write it and encourage them to write it the same way. If the question of whether they are the same comes up, I just say, "Yeah, they are aliases for each other, but I like to write them differently because I think it is clearer that way." I have yet to see anyone find that a huge hurdle. I have seen getting used to the different type of looping be a hurdle.

        As for telling people things up front, do you think that up front I tell people that blocks are really hashes, REs are really done by recursion, or many other minor details of the language? Perl is a big language, with many corners, and you shouldn't present it all at once.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://28468]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-19 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found