See if this works for you:

#!/usr/bin/env perl -w if(scalar(@ARGV) != 3){ die "Usage: dc inputfile.txt excludefile.txt outputfile.txt \n" ; } my $exclude = read_hash( $ARGV[ 1 ] ); my $dict = read_hash( $ARGV[ 2 ] ); open(OUT, ">>$ARGV[2]") or die "Error opening output file: $!\n"; open(INPUT, "$ARGV[0]") or die "Error opening input: $!\n"; while (<INPUT>) { @sentence = split(/\s+/); foreach $word (@sentence) { @count = split(//, $word); ### why split before removing \W ??? $word=~s/\W//g; next if @count < 4; ### short circuit (keep nesting down); don't ### need scalar next if $exclude->{ $word } || $dict->{ $word }; $dict->{ $word } = 1; print OUT "$word\n"; } } close OUT or die $!; close INPUT or die $!; sub read_hash { my $file = shift; open my $fh, $file or die "Error reading $file: $!\n";; chomp ( my @words = <$fh> ); close $fh or die "Error closing $file: $!\n"; return +{ map +( $_ => 1 ), @words }; }

Update: BTW, I neglected to mention that some aspects of your original code made no sense to me (though I didn't change them). Specifically, I think that instead of

@count = split(//, $word); ### why split before removing \W ??? $word=~s/\W//g; next if @count < 4; ### short circuit (keep nesting down); don't ### need scalar
what you want is something more like
$word =~ s/\W+//g; next if length $word < 4;

the lowliest monk


In reply to Re: reading/writing to a file by tlm
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.