zulqernain has asked for the wisdom of the Perl Monks concerning the following question:

@data contains the text in paragraphs and stopwords contain a list of stopwords, i wrote
foreach $word (@data) { if ($stopwords !~ / $word /) { push @lessWords, $word; } } print "@lessWords\n";
but i an getting error "Global symbol "$stopwords" requires explicit package" what does it mean?which package it require. how i can fix the problem?

Replies are listed 'Best First'.
Re: error:Global symbol "$stopwords" requires explicit package
by ikegami (Patriarch) on Jun 01, 2005 at 14:36 UTC
    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";
      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.

Re: error:Global symbol "$stopwords" requires explicit package
by wazoox (Prior) on Jun 01, 2005 at 14:35 UTC
    You probably use strict; and if you didn't, you should... That means (among some other things) that you must declare your variables with 'my' before using them (that's extremely useful against typos).
    Something like this:
    #!/usr/bin/perl use strict; # declare the variables my $stopwords = "word"; my @data = qw( a word another and a verb a noun a phrase); my @lessWords; foreach my $word (@data) { if ($stopwords !~ / $word /) { push @lessWords, $word; } } print "@lessWords\n";
Re: error:Global symbol "$stopwords" requires explicit package
by Joost (Canon) on Jun 01, 2005 at 14:35 UTC
Re: error:Global symbol "$stopwords" requires explicit package
by ghenry (Vicar) on Jun 01, 2005 at 14:29 UTC

    You need to change your declaration to:

    my $stopwords = "whatever_you_set";

    Before your code, that is.

    You must be using use strict;

    HTH.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
      i am using these statements
      open( LIST, "stopwords.txt" ) or die "$!"; my @stopwords = <LIST>;
      yes i am using strict
Re: error:Global symbol "$stopwords" requires explicit package
by Transient (Hermit) on Jun 01, 2005 at 14:31 UTC
    Because that is a snippet, we can't tell where you're defining your variables. Where is your definition of "stopwords"? If it is a list, $stopwords is a different variable from @stopwords (one is a scalar, or single value, the other is a list).

    Based upon my best guess, you need a grep, not a regexp.

    Grep is used to search for a value within a list, so you'd want something like:
    unless ( grep { / $word / } @stopwords ) { push @lessWords, $word; }
    But please click the link on grep to explore WHY that is the case.

    Update: Fixed grep logic based upon OP.