CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Frequently Asked Questions:
How do I process/modify each element of an array?

 



Jasmine
Administrator

Jan 19, 2001, 3:15 PM

Post #1 of 1 (1565 views)
How do I process/modify each element of an array? Can't Post

(From the Perl FAQ)

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

Use for/foreach:

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

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

Code
    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:

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


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives