in reply to search and replace
#!/usr/bin/perl use strict; my $old_string = "the dog chased my cat."; my ($new_string, $found_word); my %dict; while( <DATA> ) { my ($key, $val) = $_ =~ /^(\d)\s(\w+)/; $dict{$val} = $key; } chop( $old_string ); my @words = split( " ", $old_string); foreach my $i ( @words ) { foreach my $j ( keys %dict ) { if ( $j eq $i ) { $new_string = $new_string . "$dict{$j} "; $found_word++; } } $new_string = $new_string . "$i " if ($found_word == 0); $found_word = 0; } chop( $new_string ); print "$new_string."; __DATA__ 1 dog 2 cat 3 chased 4 the
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: search and replace
by jwkrahn (Abbot) on Mar 17, 2009 at 19:11 UTC | |
by Anonymous Monk on Apr 03, 2009 at 09:46 UTC | |
by Anonymous Monk on Apr 03, 2009 at 09:55 UTC |