I'm a bit confused about just what you want to delete and what you want to keep, but here's an approach. I've no idea about its speed. It needs Perl version 5.10+. Note that field offsets are zero-based.
c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $inrec = '0;1;2?foo=DUH;3;4;5;6?bar=BLAB DAB;7;8;9;;;;;;'; print qq{begin: '$inrec'}; ;; my $sep = ';'; my $data = qr{ [^\Q$sep\E] }xms; my $field = qr{ $data* \Q$sep\E }xms; ;; my %targets = qw(foo 2 bar 6); my @words = sort { $targets{$a} <=> $targets{$b} } keys %targets ; ;; for my $word (@words) { $inrec =~ s{ \A (?:$field){$targets{$word}} [^?]+ \? \Q$word\E = \K ($data*) } {}xms; $inrec .= qq{$1$sep}; print qq{'$inrec'}; } ;; print qq{final: '$inrec'}; " begin: '0;1;2?foo=DUH;3;4;5;6?bar=BLAB DAB;7;8;9;;;;;;' '0;1;2?foo=;3;4;5;6?bar=BLAB DAB;7;8;9;;;;;;DUH;' '0;1;2?foo=;3;4;5;6?bar=;7;8;9;;;;;;DUH;BLAB DAB;' final: '0;1;2?foo=;3;4;5;6?bar=;7;8;9;;;;;;DUH;BLAB DAB;'
Update: Changed code example to make more consistent use of the $sep scalar.
Give a man a fish: <%-(-(-(-<
In reply to Re: split, manipulate, join
by AnomalousMonk
in thread split, manipulate, join
by jc.smith3
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |