Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello all,
I am writing a perl script that will replace in a text words or phrases included in a dictionary file.
For instance, the dictionary file looks like:
chinese francais restaurant restaurant chinese restaurant restaurant francais
and my text like:
This is a chinese restaurantWhat I want is to replace the chinese restaurant with restaurant francais and not the chinese and restaurant separately.
My code looks like:
#read file in hash my %dictionary; while (<DIC>) { my ( $key, $tgt ) = split(/\t/, $_); push @{ $dictionary{$key} }, $tgt; } #apply dic to txt file while (<FILEIN>) { my $line = $_; foreach my $key (keys %dictionary) { #$line =~ s/%$src%/$dictionary{$key}/g; } print $line; }
I would appreciate any help
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: replace text using hash
by kennethk (Abbot) on Sep 08, 2014 at 17:10 UTC | |
by LanX (Saint) on Sep 08, 2014 at 18:10 UTC | |
by LanX (Saint) on Sep 08, 2014 at 17:29 UTC | |
by Anonymous Monk on Sep 09, 2014 at 13:33 UTC | |
by LanX (Saint) on Sep 09, 2014 at 13:57 UTC | |
by Anonymous Monk on Sep 09, 2014 at 14:12 UTC | |
by kennethk (Abbot) on Sep 09, 2014 at 15:15 UTC | |
by LanX (Saint) on Sep 09, 2014 at 15:34 UTC | |
|
Re: replace text using hash
by LanX (Saint) on Sep 08, 2014 at 16:45 UTC | |
|
Re: replace text using hash
by b4swine (Pilgrim) on Sep 09, 2014 at 01:28 UTC |