S
Sara
I have a file like
CAT 4
DOG 3
CAT 6
CAT 9
BIRD 4
DOG 13
MOUSE 2
CAT 4
DOG 3
CAT 6
CAT 9
BIRD 4
DOG 13
MOUSE 2
: [...]
: I'm trying to find lines with a certain starting word like
:
: @a = map /^((CAT|DOG).+)$/, @a;
If you're trying to find matches for a pattern, use Perl's grep
operator:
$ cat try
#! /usr/local/bin/perl
my @a = (
'CAT 4',
'DOG 3',
'CAT 6',
'CAT 9',
'BIRD 4',
'DOG 13',
'MOUSE 2',
);
@a = grep /^(CAT|DOG)\b/, @a;
print "[$_]\n" for @a;
$ ./try
[CAT 4]
[DOG 3]
[CAT 6]
[CAT 9]
[DOG 13]
$
: [...]
See the perlfunc manpage's documentation of grep for more information.
Hope this helps,
Greg
I have a file like
CAT 4
DOG 3
CAT 6
CAT 9
BIRD 4
DOG 13
MOUSE 2
.
.
I'm trying to find lines with a certain starting word like
@a = map /^((CAT|DOG).+)$/, @a;
What I WANT is actually all of the $1's (the whole line), but I get an
array of the $1 and $2's instead. In this case I only wanted to use
the parens to define the grouping of alternating substrings (as
defined on p59 in Camel 2nd ed), but as a side-effect I end up with
twice the size array as I expected.
Paul van Eldijk said:Try:
@a = map /^((?:CAT|DOG).+)$/, @a;
^^
That way, the second set of parentheses (with the ?: appended) acts as
clustering-only, without capturing.
See perldoc perlre for more info.
HTH,
Paul
Greg Bacon said:: I wish other delimiters had been chosen for alternative grouping, like
:
: <CAT|DOG>
:
: Since Alterntaive grouping is often not related to capturing $-vars..
Your wish is granted:
(?attern)
(?imsx-imsxattern)
This is for clustering, not capturing; it groups
subexpressions like "()", but doesn't make back-
references as "()" does. So
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.