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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
s/sub1/replace1/ || s/sub2/replace2/ || s/sub3/replace3/ || s/sub4/replace4/;
another might be something like
s/sub1|(sub2|(sub3|(sub4))
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?