Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I want to search in my text and replace each term with its index. I have my code and it works but I have two problems:1 eat 2 habit 3 boy 4 man-kind 5 man 6 kind ..
Where INFILE is my text and DATA is my index dictionary. Thanks in advance.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/; while (my $line = <INFILE>) { $line =~ s/(\S+)/$dict{$1} || $1/eg; print $line; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: seaching for replacement
by kennethk (Abbot) on Nov 13, 2009 at 16:40 UTC | |
|
Re: seaching for replacement
by halfcountplus (Hermit) on Nov 13, 2009 at 16:34 UTC | |
|
Re: seaching for replacement
by johngg (Canon) on Nov 13, 2009 at 19:07 UTC | |
|
Re: seaching for replacement
by toolic (Bishop) on Nov 13, 2009 at 16:42 UTC |