in reply to Replacing consecutive tokens in 1 pass

An alternate approach is to "pipeline" the process. Split the stream into tokens, operate on the tokens, and then reassemble the tokens. This might be overkill for your particular example, but is still a useful technique to have in your bag.
my $string = "|90|93|foo|bar|91|92|95|96|906|"; my $result = join "", map { s/^9[0-6]$/X/; $_ } $string =~ m/(\||[^|]+)/g; print $result; __END__ |X|X|foo|bar|X|X|X|X|906|
It's tempting to use
split /\|/ $string
to do the tokenizing, then
join "|"
to reassemble them, but you'll lose the trailing "|".

Replies are listed 'Best First'.
Re: Re: Replacing consecutive tokens in 1 pass
by ihb (Deacon) on Feb 24, 2003 at 13:10 UTC
    You can still use the split()/join() approach, if you utilize the LIMIT argument of split().
    my $string = "|90|93|foo|bar|91|92|95|96|906|"; my $result = join '|', map { s/^9[0-6]$/X/; $_ } split /\|/, $string, -1; print $result; __END__ |X|X|foo|bar|X|X|X|X|906|
    ihb