Replace substring in all values of a hash array?

Discussion in 'Perl Misc' started by Brian McCauley, May 23, 2005.

  1. michael wrote:

    > 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;
     
    Brian McCauley, May 23, 2005
    #1
    1. Advertising

  2. Brian McCauley

    Anno Siegel Guest

    michael <> wrote in comp.lang.perl.misc:
    > 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
     
    Anno Siegel, May 23, 2005
    #2
    1. Advertising

  3. Brian McCauley

    michael Guest

    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

    --
    Your object is to save the world, while still leading a pleasant life.
     
    michael, May 23, 2005
    #3
  4. Brian McCauley

    michael Guest

    > 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

    --
    Today's weirdness is tomorrow's reason why.
    -- Hunter S. Thompson
     
    michael, May 23, 2005
    #4
  5. Brian McCauley

    michael Guest

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


    Thanks fir this info - will test.

    Michael

    --
    Q: What's the difference between Bell Labs and the Boy Scouts of America?
    A: The Boy Scouts have adult supervision.
     
    michael, May 23, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  2. Anthony Martinez
    Replies:
    4
    Views:
    275
    Robert Klemme
    Jun 11, 2007
  3. Michal Suchanek
    Replies:
    6
    Views:
    234
    Nobuyoshi Nakada
    Jun 13, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
  5. Replies:
    3
    Views:
    205
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page