Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Substitution problem

by selva (Scribe)
on Jul 12, 2010 at 10:11 UTC ( #848964=perlquestion: print w/replies, xml ) Need Help??
selva has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have a small requirement in perl substitution. Following code will give output as

HAI James

#!/usr/bin/perl use strict; use warnings; my %hash = ( 'GREETING1' => 'HAI', 'GREETING2' => 'HELLO', ); my $value = '-GREETING1- James -POSITION-'; #Replace greetings value $value =~s/-(.+?)-/$hash{$1}/g; print "$value\n\n";

but I need the output like following,


because -POSITION- will be replaced by another substitution. So if hash doesn't have key then it should not replace. How do i make this in above substitution ???

Replies are listed 'Best First'.
Re: Substitution problem
by ambrus (Abbot) on Jul 12, 2010 at 10:32 UTC

    Try something like

    #Replace greetings value my $hash = sub { exists($hash{$2}) ? $hash{$2} : $1 }; $value =~s/(-(\S+?)-)/&$hash()/ge;
Re: Substitution problem
by JavaFan (Canon) on Jul 12, 2010 at 11:16 UTC
    Another way is to make a pattern that only matches the keys of your hash:
    my $pat = join '|', map "\Q$_", keys %hash; $value =~ s/-($pat)-/$hash{$1}/g;
Re: Substitution problem
by BioLion (Curate) on Jul 12, 2010 at 10:36 UTC

    A simple fix would be to do a few checks before you make the substitution:

    use strict; use warnings; use Data::Dumper qw{Dumper}; my %hash = ( 'GREETING1' => 'HAI', 'GREETING2' => 'HELLO', ); my $value = '-GREETING1- James -POSITION- -GREETING2- -FOO-'; ## find potential matches my @matches = $value =~ m/-([^\-\s]+)-/g; ## give summary print "" . (scalar@matches) . " matches for >>$value<<:\n" . (Dumper \ +@matches); ## check each potential match before substituting for my $match (@matches){ if (exists$hash{$match}){ # recognised? ## make substitution, including updates on string print "\t>$match< replaced:\n"; print "\tWAS : $value\n"; $value =~s/-$match-/$hash{$match}/; print "\tNOW : $value\n"; } else { print "$match not replaced.\n"; } }


    4 matches for >>-GREETING1- James -POSITION- -GREETING2- -FOO-<<: $VAR1 = [ 'GREETING1', 'POSITION', 'GREETING2', 'FOO' ]; >GREETING1< replaced: WAS : -GREETING1- James -POSITION- -GREETING2- -FOO- NOW : HAI James -POSITION- -GREETING2- -FOO- POSITION not replaced. >GREETING2< replaced: WAS : HAI James -POSITION- -GREETING2- -FOO- NOW : HAI James -POSITION- HELLO -FOO- FOO not replaced.

    Not a one liner, but maybe a bit clearer to follow and easier to expand later... HTH!

    Updated: Added some comments...

    Just a something something...
Re: Substitution problem
by jethro (Monsignor) on Jul 12, 2010 at 11:06 UTC
Re: Substitution problem
by JavaFan (Canon) on Jul 12, 2010 at 10:32 UTC
    s{(-(.+?)-)}{$hash{$2} // $1}eg;
    This assumes 5.10 or later.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://848964]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2017-02-24 22:11 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (363 votes). Check out past polls.