I am beginning perl user, so I'm sorry in advance if I miss some easy things, but I cannot get it to work and I am not sure whether every step is necessary(I have problems in particular with the while loops and 'saving' its outcome). I would like to do the following:

I have two text files. File 1 with a word list (one word per line) and File 2 that is a regular text document with text and numbers. I would like to count the number of words in file 2 that I can also find in file 1 and the total number of words in the document (so that I can calculate the percentage of corresponding words)

this is how far I have come:

use strict; use warnings; #Part one of the code, the wordlist is a file with one word per line a +nd I transform this into a hash my $filename = "wordlist.txt"; open(INPUT, $filename) or die "Cannot open $filename"; my $line = <INPUT>; while($line = <INPUT>){ chomp($line); my @words = split(/\s+/, $line); my %unique = map {$_ => 1 } @words; my @unique = %unique; #part two of code, open the text file and extract words only (because +the file also includes many numbers), and count the number of occuren +ce stored in a hash open (DATA, "4.txt") or die; my @UnNum; my $x; my %dict; while (<DATA>) { chomp; $_ = lc; s/ -- / /g; s/ - / /g; s/ +/ /g; s/[.,:;?"!_()\[\]]//g; my @UnNum = split(/\s+/); foreach $x(@UnNum){ if ($x =~ /(([a-zA-Z']+-)*[a-zA-Z']+)/ ){ ++$dict{$x}; }}} #part 3 of the code, I try to compare the two different hashes and add + the total number of occurrences while ((my $words,my $number) = each (%dict)) {my $total+= $number; if (exists($unique{$words})){ my $corresponding +=$number; print "There are $corresponding corresponding words of in total $total + words";}} }

thank you in advance for the help!


In reply to count number of overlapping words in a document by dmarcel

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.