I am trying to extract common words from two files, which are to be specified on the command line. The print out is to include the list of common words and the count of how many common words were found. Non-characters must be removed. Here is what I have so far, which is not working....

my $f1 = shift; my $f2 = shift; if (! defined($f1) or ! defined($f2)) { die "Need two text file names as arguments. \n"; } my %results; open my $file1, '<', $f1; while (my $line = <$file1>) { $line =~ s/[[:punct:]]//g; for my $word (split(/\s+/,$line)) { $word =~ s/[^A-Za-z0-9]//g; $results{lc $word} = 1; } } my @words2; my @storage; open my $file2, '<', $f2; while (my $line = <$file2>) { $line =~ s/[[:punct:]]/ /g; @words2 = grep { /\S/ } split(/ /,$line); for (my $i=0; $i<scalar @words2; $i++){ $words2[$i] = lc($words2[$i]); $words2[$i] =~ s/[^A-Za-z0-9]//g; push(@storage, $words2[$i]); if (grep {$_ eq $words2[$i]} @storage[0..$#storage-1]){ $results{$words2[$i]} = 1; }else{ $results{$words2[$i]}++; } } } my $counter = 0; foreach my $words (sort { $results{$b} <=> $results{$a} } keys %result +s) { if ($results{$words} > 1){ $counter = $counter+1; print $words, "\n\n" ; } } printf "Found %1.0f words in common\n", $counter;
Any help would be appreciated. Thank you.

In reply to Extracting common words by Anonymous Monk

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.