Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > aternative grouping and map, deuglification

Reply
Thread Tools

aternative grouping and map, deuglification

 
 
Sara
Guest
Posts: n/a
 
      11-25-2003
I have a file like

CAT 4
DOG 3
CAT 6
CAT 9
BIRD 4
DOG 13
MOUSE 2
 
Reply With Quote
 
 
 
 
Greg Bacon
Guest
Posts: n/a
 
      11-25-2003
In article <(E-Mail Removed) >,
Sara <(E-Mail Removed)> wrote:

: [...]
: 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
--
Ah, women. They make the highs higher and the lows more frequent.
-- Nietzsche
 
Reply With Quote
 
 
 
 
Sara
Guest
Posts: n/a
 
      11-25-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Greg Bacon) wrote in message news:<(E-Mail Removed)>...
> In article <(E-Mail Removed) >,
> Sara <(E-Mail Removed)> wrote:
>
> : [...]
> : 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
 
Reply With Quote
 
Paul van Eldijk
Guest
Posts: n/a
 
      11-26-2003
On Tue, 25 Nov 2003 08:19:54 -0800 Sara wrote:

> 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


--
$_=q{ ^4;c;14;1b:a^5;16:c^17:e^a;11;19:h^9;15:j^0:k^18:l ^13
:n^6^7^10:r^b;12;1a:s^2:t^3;8:u^1};s{(?<=[;^])(\d)?([\d
abc])}{$a=$1;$2=~/([abc])/?$a*13+ord($1)%87:$1*13+$2}egx;
for(split/:/){($a,@_)=split/[;^]/;@@[@_]=($a)x@_}print@@

 
Reply With Quote
 
Sara
Guest
Posts: n/a
 
      11-26-2003
"Paul van Eldijk" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On Tue, 25 Nov 2003 08:19:54 -0800 Sara wrote:
>
> > 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


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
 
Reply With Quote
 
Greg Bacon
Guest
Posts: n/a
 
      11-27-2003
In article <(E-Mail Removed) >,
Sara <(E-Mail Removed)> wrote:

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

@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
--
WARNING! In the considerations of safety, you should NEVER let a male
dolphin attempt anal sex with you.
-- http://www.dolphinsex.org/
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-27-2003

Greg Bacon <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed) >,
> Sara <(E-Mail Removed)> wrote:
>
> : 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


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

Ben

--
"If a book is worth reading when you are six, * (E-Mail Removed)
it is worth reading when you are sixty." - C.S.Lewis
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Port Grouping and Trunking problem hari Cisco 7 01-07-2004 09:01 AM
XSLT: sorting and grouping Christian Ludwig XML 2 11-26-2003 10:37 AM
Grouping and bypassing links Luigi Donatello Asero HTML 3 10-02-2003 04:24 PM
Grouping and Sum problem Bryce (Work) XML 3 10-01-2003 04:51 PM
Re: XSL: grouping data and group header? Frank-Ralph Reiser XML 0 07-31-2003 07:53 AM



Advertisments