Replace substring in all values of a hash array?

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::page})){
$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;
 
A

Anno Siegel

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
Irrelevant.

# 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::page})){
$class::value = "yellow";

$flora_and_fauna{$current::page} =~ s/green\.gif/yellow.gif/;
}
else {
$class::value = "green";
}

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

All values? In your code you are only considering one value at a
time.
%flora_and_fauna array. How by using regex and replace can this be done?

Add the line as indicated.

If you have a list @pages of all pages, this is more compact than
your loop:

s/green\.gif/yellow.gif/ for
@flora_and_fauna{ grep exists $flora_and_fauna{ $_}, @pages};

(Code untested)

Anno
 
M

michael

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::page})){
$class::value = "yellow";
}
else {
$class::value = "green";
}

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?

Thanks,
Michael
 
M

michael

Abusing package variables like this is bad.

I had a guilt feeling about it but wasn't able to figure variable scope in
time of getting the script functional.
substution you need the sustitution operator s/// or you can use substr().

Thanks for these useful tips.
s/_green\.gif/_yellow.gif/ for values %flora_and_fauna;

And for this, it got all working as needed.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top