B
Brian McCauley
michael said:I would like to replace substrings in hash values, for example:
%flora_and_fauna =
('z0.html' => 'flora_and_fauna_green.gif alt=flora and fauna',
'z1.html' => 'pretty_flower_green.gif alt=pretty flowers',
'z2.html' => 'deadly_vines_green.gif alt=deadly vines');
# the above values being linked images rather than text links
# sometime later in the script I run an if statement to check
# if filename is in array for setting relevant CSS html class
if(exists($flora_and_fauna{$current:age})){
$class::value = "yellow";
}
else {
$class::value = "green";
}
Abusing package variables like this is bad.
Depending on the outcome of above if block, I would like to modify the
substring "_green.gif" to become "_yellow.gif" in all values of the
%flora_and_fauna array. How by using regex and replace can this be done?
A regex is just a way of describing a pattern to match. To do a
substution you need the sustitution operator s/// or you can use substr().
s/_green\.gif/_yellow.gif/;
To apply this to all the values in %flora_and_fauna:
s/_green\.gif/_yellow.gif/ for values %flora_and_fauna;