in reply to error:Global symbol "$stopwords" requires explicit package

stopwords contain a list of stopwords,

That error means $stopwords is not declared (my or our). If you have a list of stop words, I presume it's in @stopwords rather than $stopwords? If so, you need a second loop to scan over your list of stop words.

WORD: foreach $word (@data) { foreach my $stopword (@stopwords) { next WORD if $word eq $stopword; } push(@lessWords, $word); } print "@lessWords\n";

Instead of using a second loop, you could speed up things greatly (if you have more than a couple of @words) by using a hash.

my %stopwords; # Create an element in the hash for each stop word. undef @stopwords{@stopwords}; foreach $word (@data) { next if exists $stopword{$word}; push(@lessWords, $word); } print "@lessWords\n";

Replies are listed 'Best First'.
Re^2: error:Global symbol "$stopwords" requires explicit package
by zulqernain (Novice) on Jun 01, 2005 at 14:48 UTC
    i am puting the stoplist sperated by \n from the file to the array of stopwords
    open( LIST, "stopwords.txt" ) or die "$!"; my @stopwords = <LIST>;
    as you suggested to use hash instead can you please tell me how i can put the stopwords read from file to the hash thanks

      I already did:

      open( LIST, "stopwords.txt" ) or die "$!"; my @stopwords = <LIST>; chomp(@stopwords); # <- was missing my %stopwords; # Create an element in the hash for each stop word. undef @stopwords{@stopwords};

      or using less memory:

      # Create an element in the hash for each stop word. my %stopwords; open( LIST, "stopwords.txt" ) or die "$!"; while (<LIST>) { chomp; undef $stopwords{$_}; }

      Update: Added chomp.

        now it sgiving me same error for
        my %stopwords;