Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Simple question about foreach and my.

by THRAK (Monk)
on Mar 27, 2001 at 18:35 UTC ( [id://67497]=note: print w/replies, xml ) Need Help??


in reply to Simple question about foreach and my.

In some case I find it easiest to not even use a variable, but rely on your friend: $_

foreach (@somearray) { s/for_this/sub_this/g; }


There are many times when this is useful. This might not be the best example but it is one possible use. In this case to do a substitution in each array entry. I think a bigger point would be that you have found your way to "use strict". A definate thing you should always do.

THRAK

Replies are listed 'Best First'.
Re: Re: Simple question about foreach and my.
by Tyke (Pilgrim) on Mar 27, 2001 at 18:53 UTC
    You need to be very careful using this idiom, especially if you're hoing to embed loops. It's very easy to mix up which $_ belongs to which loop and create some horrendous problems.

    In general I find it's better to name (and declare) the iteration variables unless it's really going to be a short loop (in which case I tend to use the statement modifier expr for (@array) in any case)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-23 08:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found