in reply to grep exact words
When using grep(), you are iterating over each item in the list (in this case @serv). An alias to the item is made on each pass, which is stored in $_ for you. Your example does a simple pattern match against $ARGV, so let's just change that to do an equality test:
#!perl -w use strict; my @serv = qw(foo bar baz); for my $arg (@ARGV) { # exact match (foo/foo match, but not foo/Foo) my $valid_arg = grep { $arg eq $_ } @serv; # case insensitive (foo/foo match, so do foo/Foo) # my $valid_arg = grep { lc($arg) eq $_ } @serv; die("Invalid argument '$arg'.\n") unless ($valid_arg); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: grep exact words
by Anonymous Monk on Jun 26, 2004 at 00:11 UTC | |
by Fletch (Bishop) on Jun 26, 2004 at 02:01 UTC | |
by Roy Johnson (Monsignor) on Jun 26, 2004 at 12:55 UTC | |
by Roy Johnson (Monsignor) on Jun 26, 2004 at 17:30 UTC | |
|
Re^2: grep exact words
by Anonymous Monk on Jun 26, 2004 at 00:08 UTC |