aternative grouping and map, deuglification

G

Greg Bacon

: [...]
: 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
 
S

Sara

: [...]
: 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


DUH!! Good suggestion sometimes I can't see the forest for the trees :)

Happy T-Day!

G
 
P

Paul van Eldijk

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.

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
 
S

Sara

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

Seet! Thanks for the reply Paul- a nice trick. I may have seen this
before but its one of those used so infrequently that its easily
forgotten.

I wish other delimiters had been chosen for alternative grouping, like

<CAT|DOG>

Since Alterntaive grouping is often not related to capturing $-vars..

Cheers,
G
 
G

Greg Bacon

: [...]
: 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:

(?:pattern)
(?imsx-imsx:pattern)
This is for clustering, not capturing; it groups
subexpressions like "()", but doesn't make back-
references as "()" does. So

@fields = split(/\b(?:a|b|c)\b/)

is like

@fields = split(/\b(a|b|c)\b/)

but doesn't spit out extra fields. It's also
cheaper not to capture characters if you don't
need to.

Any letters between "?" and ":" act as flags
modifiers as with "(?imsx-imsx)". For example,

/(?s-i:more.*than).*million/i

is equivalent to the more verbose

/(?:(?s-i)more.*than).*million/i

I even took time to modify your perlre manpage accordingly.

Just another Perl djini,
Greg
 
B

Ben Morrow

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:

(?:pattern)
(?imsx-imsx:pattern)
This is for clustering, not capturing; it groups
subexpressions like "()", but doesn't make back-
references as "()" does. So

And no, this is Not Good, as (?: ) is more often used than ( ). This
will be remedied in Perl6.

Ben
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top