Hello
I've got two text files

c.txt:

Here I am this is me Theres nowhere else on earth Id rather be
d.txt:
Here noun am verb Theres advb
I'm storing the contents of d.txt in a hash, where the words are the keys and the grammatical categories are the values.

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


In reply to File write by perl_seeker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.