Mr. Shawn H. Corey wrote:
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?
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; }
You are lucky that /^U.S.C$/ will match 'U.S.C' but if the string
contained other regexp meta-characters it probably would not have
matched, and it will also match strings that are not 'U.S.C'. To match
exactly you need to use quotemeta:
next if grep /\A\Q$lastword\E\z/, @ignore;
Or better yet just use an equality test:
next if grep $_ eq $lastword, @ignore;
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/