On Sat, 2008-08-02 at 06:31 -0700, hsfrey wrote:
> I'm trying to set up a list of words to ignore in a text.
> I tried it like this:
> 
> my @ignore = ("U.S.C", "Corp", "Miss", "Conf", "Cong");
> 
> and later in a loop
> 
> if ( $exists $ignore [$lastWord] ) { next;}
> 
> But that tested positive for EVERY $lastWord and skipped every time !
> It did the same when I used "defined" instead of "exists".
> 
> I finally got it to work with this kludge:
> 
> my %ignore = ("U.S.C"=>5, "Corp"=>5, "Miss"=>5, "Conf"=>5, "Cong"=>5);
>       if ( $ignore{$lastWord} eq 5) { next;}
> 
> Why didn't exists and defined work?
> 
> Harvey
> 
> 

The parameter for an array must be a number.  If $lastword does not
contain a number, perl decides that it is zero.  The expression
$ignore[$lastword] would be "U.S.C", which exists and is defined.

I think you want something like this:

  if( grep { /^$lastword$/ } @ignore ){ next; }

See `perldoc -f grep` for details.

You should always place these two lines at the beginning of your
scripts:

  use strict;
  use warnings;

They would have warned you that $lastword is not numeric.


-- 
Just my 0.00000002 million dollars worth,
  Shawn

"Where there's duct tape, there's hope."

"Perl is the duct tape of the Internet."
        Hassan Schroeder, Sun's first webmaster


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to