ionstorm101
Programmer
Hi, im writing a script to create lists of words from an input file. The script im writing is meant to take 3 arguments, an input file, a list of words not to be included in the dictionary and an output file. The problem is I want no repeated words in the dictionary. What im trying to do is check the file im writing (the output file) to to see if the word is already in it and if it isnt then add it.
My code never enters this check though i.e the while(<OUT>) and I cant tell why. I have the file opened for both read and write. Here is my script so far. If anyone could help that would be great. Im confused as it has no problem checking the word against anything in the file of words not to allow in i.e the while(<EXCLUDE>) part.
My code never enters this check though i.e the while(<OUT>) and I cant tell why. I have the file opened for both read and write. Here is my script so far. If anyone could help that would be great. Im confused as it has no problem checking the word against anything in the file of words not to allow in i.e the while(<EXCLUDE>) part.
Code:
#!/usr/bin/env perl -w
if(scalar(@ARGV) != 3){
die "Usage: dc inputfile.txt excludefile.txt outputfile.txt \n" ;
}
open(INPUT, "$ARGV[0]") or die "Error opening input: $!\n";
#And now we start to go through the file
while(<INPUT>){ #copy in lines as strings
@sentance = split(/\s+/); #split at any whitespace, tab, newline etc
foreach $word (@sentance){
@count = split(//, $word);
$word=~s/\W//g;
if((scalar(@count)) >= 4){
$allow = 1;
open(EXCLUDE, "$ARGV[1]") or die "Error opening exclusion file: $!\n";
open(OUT, "+>>$ARGV[2]") or die "Error opening output file: $!\n";
while(<EXCLUDE>){
chomp $_;
if($word eq $_){
print "match \n";
$allow = 0
}
}
while(<OUT>){ #getting in here is the problem
print "in the zone \n"; #print this if we get in here
chomp $_;
if($word eq $_){
$allow = 0;
}
}
if($allow == 1){
print "$word\n";
}else{
print "allow is $allow\n";
}
print OUT "$word\n" if $allow == 1;
close(EXCLUDE);
close(OUT);
}