Think about Loose Coupling PerlMonks

### Re: Substitution problem

by BioLion (Curate)
 on Jul 12, 2010 at 10:36 UTC ( #848972=note: print w/replies, xml ) Need Help??

in reply to Substitution problem

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"; }
}

[download]```

Gives:

```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.
[download]```

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

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://848972]
help
Chatterbox?
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-06-23 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (534 votes). Check out past polls.