in reply to match lines containing state abbreviation
G'day PerlSufi,
In a boolean context, any non-zero-length string evaluates to TRUE. So the grep expression will always be TRUE and @keepers will be a copy of @lines.
$ perl -Mstrict -Mwarnings -E ' my $match = "AZ"; my @lines = qw{AZ SX AZ DC}; my @keepers = grep { $match } @lines; say "@keepers"; ' AZ SX AZ DC
What you need to do to filter @lines, is make the grep expression a regular expression (i.e. /$match/).
$ perl -Mstrict -Mwarnings -E ' my $match = "AZ"; my @lines = qw{AZ SX AZ DC}; my @keepers = grep { /$match/ } @lines; say "@keepers"; ' AZ AZ
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: match lines containing state abbreviation
by PerlSufi (Friar) on Apr 18, 2013 at 18:04 UTC | |
|
Re^2: match lines containing state abbreviation
by PerlSufi (Friar) on Apr 18, 2013 at 17:47 UTC | |
by kcott (Archbishop) on Apr 18, 2013 at 18:20 UTC |