How about something like this? Ignores any punctuation except for internal apostrophes (contractions), works with any Unicode words, not just ASCII or Latin-1.

#! /usr/bin/perl use warnings; use strict; if(scalar(@ARGV) != 3){ die "Usage: $0 inputfile.txt excludefile.txt outputfile.txt\n" ; } my $word = qr/(?<!\p{Alnum})\p{Alnum}+(?!\p{Alnum})/; my %excluded; open my $exclude, '<', $ARGV[1] or die "Error opening exclude file: $! +\n"; while (my $line = <$exclude>) { while ($line =~ /($word('$word)?)/g) { undef $excluded{$1}; } } close $exclude; my %included; open my $input, '<', $ARGV[0] or die "Error opening input file: $!\n"; while (my $line = <$input>) { while ($line =~ /($word('$word)?)/g) { undef $included{$1} unless exists $excluded{$1}; } } close $input; open my $output, '>>', $ARGV[2] or die "Error opening output file: $!\ +n"; print $output join "\n", sort keys %included;

Update: Changed open for output to append instead of truncate, as pointed out by tlm

After looking at this and the OPs post again, I realized that I left out the check for word less than 4 letters and would still possibly (probably) end up with dupe words in the output file. This fixes those problems.

#! /usr/bin/perl use warnings; use strict; if(scalar(@ARGV) != 3){ die "Usage: $0 inputfile.txt excludefile.txt outputfile.txt\n" ; } my $word = qr/(?<!\p{Alnum})\p{Alnum}+(?!\p{Alnum})/; my %excluded; open my $exclude, '<', $ARGV[1] or die "Error opening exclude file: $! +\n"; while (my $line = <$exclude>) { while ($line =~ /($word('$word)?)/g) { undef $excluded{$1}; } } close $exclude; my %included; open my $input, '<', $ARGV[0] or die "Error opening input file: $!\n"; while (my $line = <$input>) { while ($line =~ /($word('$word)?)/g) { next if length $1 < 4; undef $included{$1} unless exists $excluded{$1}; } } close $input; %excluded = (); open my $output, '<', $ARGV[2] or die "Error opening dictionary file: +$!\n"; while (my $line = <$output>) { while ($line =~ /($word('$word)?)/g) { undef $included{$1}; } } close $output; open $output, '>', $ARGV[2] or die "Error opening output file: $!\n"; print $output join "\n", sort keys %included;

In reply to Re: reading/writing to a file by thundergnat
in thread reading/writing to a file by nnp

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.