Hi everyone
I'm frsutrated by this problem and would really appreciate a hand. I may not be doing this the best way, so any suggestions are more than welcome...I want to replace certain sections of a string with other sections (substring replacements).
For example, I would like to go from something like this :
<TAG val="XXX">a hundred</TAG><TAG val="XXX">a thousand</TAG>
to something like this :
<TAG val="900">nine hundred</TAG><TAG val="1000">a thousand</TAG>
Instead I am getting something like this :
<TAG val="1000">nine hundred</TAG><TAG val="1000">a thousand</TAG>
The value which is used in the substitution is always the last match. Now, the puzzling thing is, I do this in other parts of my code and it seems to work. Is there a more reliable/safer way to do this, knowing that a global replace will not do and I am reading lines in a file one at a time. Each line has possibly multiple tags of the same type.
To give you an idea, what I'm doing is something like this :
$text = "<TAG val="XXX">a hundred</TAG><TAG val="XXX">a thousand</TAG>";
while ($text =~ m/val="XXX"/) {
$text = &replace($text);
print $text; # or whatever
}
sub replace {
if ($text =~ val="XXX">(.+)</TAG> {
$val = &convert($1);
$text =~ s/val="XXX"/val="$val"/;
}
}
&covert converts the value between tags to the desired format (ie from words to numerical format).
THANKS VERY MUCH for any comments/tips!
I'm frsutrated by this problem and would really appreciate a hand. I may not be doing this the best way, so any suggestions are more than welcome...I want to replace certain sections of a string with other sections (substring replacements).
For example, I would like to go from something like this :
<TAG val="XXX">a hundred</TAG><TAG val="XXX">a thousand</TAG>
to something like this :
<TAG val="900">nine hundred</TAG><TAG val="1000">a thousand</TAG>
Instead I am getting something like this :
<TAG val="1000">nine hundred</TAG><TAG val="1000">a thousand</TAG>
The value which is used in the substitution is always the last match. Now, the puzzling thing is, I do this in other parts of my code and it seems to work. Is there a more reliable/safer way to do this, knowing that a global replace will not do and I am reading lines in a file one at a time. Each line has possibly multiple tags of the same type.
To give you an idea, what I'm doing is something like this :
$text = "<TAG val="XXX">a hundred</TAG><TAG val="XXX">a thousand</TAG>";
while ($text =~ m/val="XXX"/) {
$text = &replace($text);
print $text; # or whatever
}
sub replace {
if ($text =~ val="XXX">(.+)</TAG> {
$val = &convert($1);
$text =~ s/val="XXX"/val="$val"/;
}
}
&covert converts the value between tags to the desired format (ie from words to numerical format).
THANKS VERY MUCH for any comments/tips!