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
what you want is something more like@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
$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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |