Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^6: grep, map vs. foreach performance

by Flexx (Pilgrim)
on Sep 04, 2002 at 16:46 UTC ( [id://195127]=note: print w/replies, xml ) Need Help??


in reply to (jeffa) 5Re: grep, map vs. foreach performance
in thread grep, map vs. foreach performance

Hmm.. You missed my point (or you're repeating what I said). I wasn't comparing for(ARRAY) with for(TEMP_ARRAY), but for(ARRAY) with for(EXPR;EXPR;EXPR).

I do know that for and foreach are synonymous words. I was trying to clarify that in

for (@array) {$_++}

$_ is an implicit alias for the current @array element. While in

for (0 .. $#array) {$array[$_]++}

$_ an alias for the current index. And that, in terms of what to use for what (as you already clarified) was the difference I meant...

Replies are listed 'Best First'.
Re^7: grep, map vs. foreach performance
by sauoq (Abbot) on Sep 04, 2002 at 21:40 UTC
    I do know that for and foreach are synonymous words. I was trying to clarify that in
    for (@array) {$_++}
    $_ is an implicit alias for the current @array element. While in
    for (0 .. $#array) {$array[$_]++}
    $_ an alias for the current index.

    In your second example there, perl has no idea that $_ is an alias for the "current index." All it knows is that it is an alias for the current list element. The difference between the two examples is only in the contents of the list that the for statement is iterating over.

    -sauoq
    "My two cents aren't worth a dime.";
    

      Of course has no idea about what (semanticly) it's an alias for. I just had to find a word (or two) for that anonymous array element (since the range operator creates a temporary array, not a list) it represents, which was an index in our case. I thought that was obvious by not naming it $current_index. ;)

      It's funny that you even brought this up. Seems like Perl has so much magic that one has to point out: "Well, no, Perl does not really understand what you're doing"...

      That aside, I always thought $_ knows far too much! ;)

      So long,
      Flexx

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found