Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How do I process/modify each element of an array?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:20 UTC ( #616=perlfaq nodetype: print w/ replies, xml ) Need Help??

Current Perl documentation can be found at

Here is our local, out-dated (pre-5.6) version:

Use for/foreach:

    for (@lines) {
        s/foo/bar/;     # change that word
        y/XZ/ZX/;       # swap those letters

Here's another; let's compute spherical volumes:

    for (@volumes = @radii) {   # @volumes has changed parts
        $_ **= 3;
        $_ *= (4/3) * 3.14159;  # this will be constant folded

If you want to do the same thing to modify the values of the hash, you may not use the values function, oddly enough. You need a slice:

    for $orbit ( @orbits{keys %orbits} ) {
        ($orbit **= 3) *= (4/3) * 3.14159; 

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2015-11-30 03:45 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (757 votes), past polls