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.
#!/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); }

In reply to 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.