in reply to Re: search and replace
in thread search and replace
How about like this:
#!/usr/bin/perl use warnings; use strict; my $old_string = 'the dog chased my cat.'; my %dict; while ( <DATA> ) { my ( $key, $val ) = /^(\d+)\s+(\w+)/; $dict{ $val } = $key; } my $cc = join '', keys %dict; my ( $min ) = my ( $max ) = map length, keys %dict; for ( map length, keys %dict ) { $min = $_ if $min > $_; $max = $_ if $max < $_; } my $pattern = qr/\b([$cc]{$min,$max})\b/; ( my $new_string = $old_string ) =~ s/$pattern/ exists $dict{ $1 } ? $ +dict{ $1 } : $1 /eg; print "$old_string\n$new_string\n"; __DATA__ 1 dog 2 cat 3 chased 4 the
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: search and replace
by Anonymous Monk on Apr 03, 2009 at 09:46 UTC | |
by Anonymous Monk on Apr 03, 2009 at 09:55 UTC |