<?xml version="1.0" encoding="windows-1252"?>
<node id="616" title="How do I process/modify each element of an array?" created="1999-10-07 20:20:52" updated="2005-08-15 10:02:47">
<type id="834">
perlfaq nodetype</type>
<author id="519">
faq_monk</author>
<data>
<field name="doctext">


&lt;P&gt;
Use &lt;CODE&gt;for&lt;/CODE&gt;/&lt;CODE&gt;foreach&lt;/CODE&gt;:

&lt;P&gt;
&lt;PRE&gt;    for (@lines) {
        s/foo/bar/;     # change that word
        y/XZ/ZX/;       # swap those letters
    }
&lt;/PRE&gt;
&lt;P&gt;
Here's another; let's compute spherical volumes:

&lt;P&gt;
&lt;PRE&gt;    for (@volumes = @radii) {   # @volumes has changed parts
        $_ **= 3;
        $_ *= (4/3) * 3.14159;  # this will be constant folded
    }
&lt;/PRE&gt;
&lt;P&gt;
If you want to do the same thing to modify the values of the hash, you may
not use the &lt;U&gt;values&lt;/U&gt; function, oddly enough. You need a slice:

&lt;P&gt;
&lt;PRE&gt;    for $orbit ( @orbits{keys %orbits} ) {
        ($orbit **= 3) *= (4/3) * 3.14159; 
    }
&lt;/PRE&gt;
&lt;P&gt;
</field>
</data>
</node>
