Link to home
Start Free TrialLog in
Avatar of hadrons
hadrons

asked on

Problem with one substitution modifying another

I have a data file with problems similar to this:

C??H ACTIVATION
[?]
Modeling ?? f and
 ??-AMINO ACIDS
C??H ACTIVATION
Book?s
book?s
bio?chemical
bio?syn?chemical
ma?ana
na?ve

Where the question marks are non-ASCII characters that were converted to ? when the data was saved to a text file. I have to ferret out these examples, but the problem I'm having with my substitution script is that one substitution command is effect another.

So for example, this line in the script:

my $text;

if ($text !~ /GREP0/) {s/([A-Za-z0-9]* *[A-Za-z0-9]* \?\?[^A-Z0-9]+[^ ]+)/\nGREP01a\: $1 \n/g;}

will change this text:
Modeling ?? f and A
to
GREP01a: Modeling ?? f and A

Which is exactly what I want, but I'm find out another command line - there are 15 or so - is matching that and changing it.

Basically, if a substitution is made then I don't want that string touched by the other substitution commands. I thought adding the if ($text !~ /GREP0/) - since the substitution would add GREP0 to any string changed - would solve this, but this hasn't been the case.
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
one way could be something like
  s/sub1/replace1/ || s/sub2/replace2/ || s/sub3/replace3/ || s/sub4/replace4/;
another might be something like
  s/sub1|(sub2|(sub3|(sub4)))/${[qw(replace1 replace2 replace3 replace4)]}[!!$1+!!$2+!!$3]/;
but could you give an example of when  adding the if ($text !~ /GREP0/) - since the substitution would add GREP0 to any string changed - did not solve the case?
is it because you are testing $text and changing $_? or is it because one of the substitutions did not add GREP0?
Avatar of hadrons
hadrons

ASKER

Thanks to the both of you - you were both correct about me not setting the default variable correctly. I tested both by changing by adding my $text = $_; and then later on changing the if statements to unless and I liked the unless results better.