perl_seeker has asked for the wisdom of the Perl Monks concerning the following question:
c.txt:
d.txt:Here I am this is me Theres nowhere else on earth Id rather be
I'm storing the contents of d.txt in a hash, where the words are the keys and the grammatical categories are the values.Here noun am verb Theres advb
The contents of c.txt are stored in a hash slice, where the words are the keys.
I need to find words common to both c.txt and d.txt, that is common keys in both the hashes,and for each match write the gramm. category in c.txt next to the matched word.
I've tried some code,I think the prob. is at the end i.e. the file write to c.txt.The contents of c.txt are deleted and all I get is an empty file.
Any help would be appreciated.Thanx in advance.
open DF, 'd.txt' or die $!; my %tagdict; while (<DF>) { chomp; my ($word, $cat) = split; $tagdict{$word} = $cat; } while ( ($k,$v) = each %tagdict ) { #print "$k => $v\n"; } close DF; open CF, 'c.txt' or die $!; my %tagcorpus; while (<CF>) { chomp; my @cwords = split; @tagcorpus{@cwords} = (1) x @cwords; } while ( ($k,$v) = each %tagcorpus ) { #print "$k => $v\n"; } close CF; open CF, '>c.txt' or die $!; foreach (keys %tagdict){ print values %tagdict if exists $tagcorpus{$_}; }
update (broquaint): put text file examples in <code> tags and cleaned up formatting
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File write
by BrowserUk (Patriarch) on Jun 26, 2003 at 11:58 UTC | |
by perl_seeker (Scribe) on Jun 27, 2003 at 11:27 UTC | |
by BrowserUk (Patriarch) on Jun 27, 2003 at 11:53 UTC | |
by perl_seeker (Scribe) on Jul 02, 2003 at 11:54 UTC | |
by BrowserUk (Patriarch) on Jul 02, 2003 at 14:02 UTC | |
|