CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Text substitution failing

 



perlfarmer
User

Jul 9, 2009, 2:39 PM

Post #1 of 6 (851 views)
Text substitution failing Can't Post

while ($apar =~ /(sysroute.*\(?(\w{2}\d{5}|\d{8})\)?\s*\S*\s*)/i) {
my $firstMatch = $1;
if (($currDefectState eq "Canceled") or ($currDefectState eq "Returned" and $currDefectAnswer !~ /duplicate/i)) {
print "Nulling '$firstMatch' in APAR field of $currDefectID\n";
$apar =~ s/$firstMatch//; #Removes occurrence of $firstMatch from $apar
$recordUpdated = 1; #true
}
else {
print "Action required for $currDefectID: $firstMatch\n";
$apar =~ s/$firstMatch//; #Does not remove occurrence of $firstMatch from $apar
}
}

I am executing a script that must look for patterns in a text field called "apar." Because these patterns might be duplicated I am using a while loop to find the matches and using a substitution to remove those matches from the field.

Print statements have confirmed that, when the if-clause is executed, the substitution removes the matched value from the field. However, when the else-clause executes, the substitution does NOT remove the matched values and, since the pattern is not updated, an infinite loop occurs.

The reason for using the variable $firstMatch is that one of the tests in the if-clause has the side effect of updating $1; so I save a copy of the original value.

Why is the substitution working in the if-clause and not in the else-clause?! Any advice is greatly appreciated.


ichi
User

Jul 9, 2009, 6:13 PM

Post #2 of 6 (846 views)
Re: [perlfarmer] Text substitution failing [In reply to] Can't Post

show sample input file, put your code in code tags. describe your desired output clearly.


perlfarmer
User

Jul 10, 2009, 7:10 AM

Post #3 of 6 (831 views)
Re: [ichi] Text substitution failing [In reply to] Can't Post

To simplify things I wrote a smaller loop that just looks for successful and failed substitutions:


Code
while ($apar =~ /(sysroute.*\(?(\w{2}\d{5}|\d{8})\)?\s*\S*\s*)/i) { 
my $original = $apar;
$apar =~ s/$firstMatch//;
if ($apar eq $original) {
print "Failure: '$firstMatch' not removed from '$original'\n";
$apar = ""; #Prevent infinite loop
}
else {
print "Success: '$firstMatch' removed from '$original'\n";
}
}

Code
 
Here is a sample of the output.
Clearly every $firstMatch that has a trailing semi-colon is not substituted.
No $firstMatch without the trailing semi-colon fails to get substituted.
This holds true for more than 1000 lines of output.
So the trailing semi-colon appears to the be issue but I can't figure out why that would be the case.
Clearly the semi-colon is included in the match since it shows up in both $firstMatch and $original.

Failure: 'SYSRoute of (IZ53940) needed;' not removed from 'SYSRoute of (IZ53940) needed;'
Failure: 'SYSRoute of (SYSROUTE OF JR27541) needed;' not removed from 'SYSRoute of (SYSROUTE OF JR27541) needed;'
Success: 'SYSROUTE of IZ08901 required' removed from 'SYSROUTE of IZ08901 required'
Success: 'SYSROUTE of IZ16031' removed from 'Needs SYSROUTE of IZ16031'
Failure: 'SYSRoute of (IZ44587) needed;' not removed from 'SYSRoute of (IZ44587) needed;'
Failure: 'SYSRoute of (IZ39300) needed;' not removed from 'SYSRoute of (IZ39300) needed;'
Success: 'SYSROUTE of JR31261 required' removed from 'SYSROUTE of JR31261 required'


perlfarmer
User

Jul 10, 2009, 7:16 AM

Post #4 of 6 (830 views)
Re: [perlfarmer] Text substitution failing [In reply to] Can't Post

I spoke too soon. Here is a pattern without a trailing semi-colon that also failed the substitution:

Failure: 'SYSROUTE of (IZ47297) required' not removed from 'SYSROUTE of (IZ47297) required'


perlfarmer
User

Jul 10, 2009, 7:27 AM

Post #5 of 6 (828 views)
Re: [perlfarmer] Text substitution failing [In reply to] Can't Post

What the heck, here's 90 lines of output. I'm completely stumped.

Failure: 'SYSRoute of (JR32477) needed;' not removed from 'SYSRoute of (JR32477) needed;'
Failure: 'SYSRoute of (LI74157) needed;' not removed from 'SYSRoute of (LI74157) needed;'
Failure: 'SYSRoute of (IZ42743) needed;' not removed from 'SYSRoute of (IZ42743) needed;'
Success: 'SYSROUTE of IZ38924 required' removed from 'SYSROUTE of IZ38924 required'
Success: 'SYSROUTE of IZ22698 required' removed from 'SYSROUTE of IZ22698 required'
Success: 'SYSROUTE of JR30122 required' removed from 'SYSROUTE of JR30122 required'
Failure: 'SYSRoute of (IZ51442) needed;' not removed from 'SYSRoute of (IZ51442) needed;'
Success: 'SYSROUTE of IZ42650 required' removed from 'SYSROUTE of IZ42650 required'
Success: 'SYSROUTE of LI72137 required' removed from 'SYSROUTE of LI72137 required'
Success: 'SYSROUTE of LI72137 required' removed from 'SYSROUTE of LI72137 required'
Failure: 'SYSRoute of (LI74307) needed;' not removed from 'SYSRoute of (LI74307) needed;'
Failure: 'SYSRoute of (LI74307) needed;' not removed from 'SYSRoute of (LI74307) needed;'
Failure: 'SYSRoute of (IZ25720) needed;' not removed from 'SYSRoute of (IZ25720) needed;'
Success: 'SYSROUTE OF IY90204' removed from 'SYSROUTE OF IY90204'
Success: 'SYSROUTE OF LI73486' removed from 'SYSROUTE OF LI73486'
Success: 'SYSROUTE of JR30653 required' removed from 'SYSROUTE of JR30653 required'
Failure: 'SYSRoute of (LI73486) needed;' not removed from 'SYSRoute of (LI73486) needed;'
Failure: 'SYSRoute of (IZ40872) needed;' not removed from 'SYSRoute of (IZ40872) needed;'
Failure: 'SYSRoute of (IC61568) needed;' not removed from 'SYSRoute of (IC61568) needed;'
Success: 'SYSROUTE of IZ08435 required' removed from 'SYSROUTE of IZ08435 required'
Success: 'SYSROUTE of IZ39947 required' removed from 'SYSROUTE of IZ39947 required'
Success: 'SYSROUTE OF IZ43246' removed from 'SYSROUTE OF IZ43246'
Failure: 'SYSRoute of (IZ26907) needed;' not removed from 'SYSRoute of (IZ26907) needed;'
Failure: 'SYSRoute of (IZ45674) needed;' not removed from 'SYSRoute of (IZ45674) needed;'
Failure: 'SYSRoute of (IZ52694) needed;' not removed from 'SYSRoute of (IZ52694) needed;'
Failure: 'SYSRoute of (IZ49694) needed;' not removed from 'SYSRoute of (IZ49694) needed;'
Failure: 'SYSRoute of (IZ40920) needed;' not removed from 'SYSRoute of (IZ40920) needed;'
Failure: 'SYSRoute of (IZ42879) needed;' not removed from 'SYSRoute of (IZ42879) needed;'
Failure: 'SYSRoute of (JR33217) needed;' not removed from 'SYSRoute of (JR33217) needed;'
Failure: 'SYSRoute of (IZ43140) needed;' not removed from 'SYSRoute of (IZ43140) needed;'
Failure: 'SYSRoute of (IZ45668) needed;' not removed from 'SYSRoute of (IZ45668) needed;'
Failure: 'SYSRoute of (IZ53000) needed;' not removed from 'SYSRoute of (IZ53000) needed;'
Success: 'SYSROUTE of JR27870 required' removed from 'SYSROUTE of JR27870 required'
Success: 'SYSROUTE OF IY73680' removed from 'SYSROUTE OF IY73680'
Success: 'SYSROUTE of JR32252 required' removed from 'SYSROUTE of JR32252 required'
Failure: 'SYSRoute of (JR29447) needed;' not removed from 'SYSRoute of (JR29447) needed;'
Failure: 'SYSRoute of (IZ47512) needed;' not removed from 'SYSRoute of (IZ47512) needed;'
Failure: 'SYSRoute of (JR30022) needed;' not removed from 'SYSRoute of (JR30022) needed;'
Failure: 'SYSRoute of (IZ48106) needed;' not removed from 'SYSRoute of (IZ48106) needed;'
Success: 'SYSROUTE of IZ28300 required' removed from 'SYSROUTE of IZ28300 required'
Failure: 'SYSRoute of (IZ46871) needed;' not removed from 'SYSRoute of (IZ46871) needed;'
Failure: 'SYSRoute of (IZ50833) needed;' not removed from 'SYSRoute of (IZ50833) needed;'
Failure: 'SYSRoute of (IZ53473) needed;' not removed from 'SYSRoute of (IZ53473) needed;'
Failure: 'SYSRoute of (IC61812) needed;' not removed from 'SYSRoute of (IC61812) needed;'
Success: 'SYSROUTE OF JR31959' removed from 'SYSROUTE OF JR31959'
Success: 'SYSROUTE of IZ03898 required' removed from 'SYSROUTE of IZ03898 required'
Failure: 'SYSRoute of (LI74683) needed;' not removed from 'SYSRoute of (LI74683) needed;'
Success: 'SYSROUTE of IZ23718 required' removed from 'SYSROUTE of IZ23718 required'
Failure: 'SYSROUTE of (IZ47297) required' not removed from 'SYSROUTE of (IZ47297) required'
Failure: 'SYSRoute of (JR32339) needed;' not removed from 'SYSRoute of (JR32339) needed;'
Success: 'SYSROUTE of JR29956 required' removed from 'SYSROUTE of JR29956 required'
Failure: 'SYSRoute of (IZ50434) needed;' not removed from 'SYSRoute of (IZ50434) needed;'
Success: 'SYSROUTE of JR32644 required' removed from 'SYSROUTE of JR32644 required'
Failure: 'SYSRoute of (JR32659) needed;' not removed from 'SYSRoute of (JR32659) needed;'
Failure: 'SYSRoute of (IZ29695) needed;' not removed from 'SYSRoute of (IZ29695) needed;'
Failure: 'SYSRoute of (IZ47682) needed;' not removed from 'SYSRoute of (IZ47682) needed;'
Failure: 'SYSRoute of (JR31985) needed;' not removed from 'SYSRoute of (JR31985) needed;'
Failure: 'SYSRoute of (IZ53855) needed;' not removed from 'SYSRoute of (IZ53855) needed;'
Failure: 'SYSRoute of (IZ53555) needed;' not removed from 'SYSRoute of (IZ53555) needed;'
Failure: 'SYSRoute of (IZ53635) needed;' not removed from 'SYSRoute of (IZ53635) needed;'
Failure: 'SYSRoute of (IZ53858) needed;' not removed from 'SYSRoute of (IZ53858) needed;'
Failure: 'SYSRoute of (IZ11324) needed;' not removed from 'SYSRoute of (IZ11324) needed;'
Success: 'SYSROUTE OF JR30492' removed from 'SYSROUTE OF JR30492'
Failure: 'SYSRoute of (IZ53940) needed;' not removed from 'SYSRoute of (IZ53940) needed;'
Failure: 'SYSRoute of (SYSROUTE OF JR27541) needed;' not removed from 'SYSRoute of (SYSROUTE OF JR27541) needed;'
Success: 'SYSROUTE of IZ08901 required' removed from 'SYSROUTE of IZ08901 required'
Success: 'SYSROUTE of IZ16031' removed from 'Needs SYSROUTE of IZ16031'
Failure: 'SYSRoute of (IZ44587) needed;' not removed from 'SYSRoute of (IZ44587) needed;'
Failure: 'SYSRoute of (IZ39300) needed;' not removed from 'SYSRoute of (IZ39300) needed;'
Success: 'SYSROUTE of JR31261 required' removed from 'SYSROUTE of JR31261 required'
Failure: 'SYSRoute of (JR32763) needed;' not removed from 'SYSRoute of (JR32763) needed;'
Success: 'SYSROUTE OF IY61387' removed from 'SYSROUTE OF IY61387'
Success: 'SYSROUTE of IZ30204 required' removed from 'SYSROUTE of IZ30204 required'
Failure: 'SYSRoute of (LI74418) needed;' not removed from 'SYSRoute of (LI74418) needed;'
Success: 'SYSROUTE of IZ27170 required' removed from 'SYSROUTE of IZ27170 required'
Failure: 'SYSRoute of (IZ18634) needed;' not removed from 'SYSRoute of (IZ18634) needed;'
Success: 'SYSROUTE of IZ40343 required' removed from 'SYSROUTE of IZ40343 required'
Success: 'SYSROUTE of IZ04242 required' removed from 'SYSROUTE of IZ04242 required'
Failure: 'SYSRoute of (IZ04243) needed;' not removed from 'SYSRoute of (IZ04243) needed;'
Success: 'SYSROUTE of JR28201 required' removed from 'SYSROUTE of JR28201 required'
Success: 'SYSROUTE of IZ12018 required' removed from 'SYSROUTE of IZ12018 required'
Failure: 'SYSRoute of (JR31932) needed;' not removed from 'SYSRoute of (JR31932) needed;'
Failure: 'SYSRoute of (JR32567) needed;' not removed from 'SYSRoute of (JR32567) needed;'
Success: 'SYSROUTE of IZ39542 required' removed from 'SYSROUTE of IZ39542 required'
Success: 'SYSROUTE of IZ33965 required' removed from 'SYSROUTE of IZ33965 required'
Failure: 'SYSRoute of (JR30500) needed;' not removed from 'SYSRoute of (JR30500) needed;'
Failure: 'SYSRoute of (IZ43647) needed;' not removed from 'SYSRoute of (IZ43647) needed;'
Failure: 'SYSRoute of (IZ36946) needed;' not removed from 'SYSRoute of (IZ36946) needed;'
Failure: 'SYSRoute of (IC61962) needed;' not removed from 'SYSRoute of (IC61962) needed;'
Success: 'SYSROUTE of IZ52433 required' removed from 'SYSROUTE of IZ52433 required'


perlfarmer
User

Jul 10, 2009, 7:36 AM

Post #6 of 6 (826 views)
Re: [perlfarmer] Text substitution failing [In reply to] Can't Post

[slaps forehead] So the successful substitutions don't have any parentheses in the pattern and the failures do.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives