Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   aternative grouping and map, deuglification (http://www.velocityreviews.com/forums/t883861-aternative-grouping-and-map-deuglification.html)

Sara 11-25-2003 04:19 PM

aternative grouping and map, deuglification
 
I have a file like

CAT 4
DOG 3
CAT 6
CAT 9
BIRD 4
DOG 13
MOUSE 2

Greg Bacon 11-25-2003 05:08 PM

Re: aternative grouping and map, deuglification
 
In article <776e0325.0311250819.7fa75087@posting.google.com >,
Sara <genericax@hotmail.com> 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

Sara 11-25-2003 11:46 PM

Re: aternative grouping and map, deuglification
 
gbacon@hiwaay.net (Greg Bacon) wrote in message news:<vs733ioshhni01@corp.supernews.com>...
> In article <776e0325.0311250819.7fa75087@posting.google.com >,
> Sara <genericax@hotmail.com> 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

Paul van Eldijk 11-26-2003 09:38 AM

Re: aternative grouping and map, deuglification
 
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:o^7:p^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@@


Sara 11-26-2003 03:12 PM

Re: aternative grouping and map, deuglification
 
"Paul van Eldijk" <rev_1318@hotmail.com> wrote in message news:<pan.2003.11.26.09.38.56.790585@hotmail.com>. ..
> 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

Greg Bacon 11-27-2003 01:26 PM

Re: aternative grouping and map, deuglification
 
In article <776e0325.0311260712.7b0c39cf@posting.google.com >,
Sara <genericax@hotmail.com> 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:

(?: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
--
WARNING! In the considerations of safety, you should NEVER let a male
dolphin attempt anal sex with you.
-- http://www.dolphinsex.org/

Ben Morrow 11-27-2003 02:54 PM

Re: aternative grouping and map, deuglification
 

Greg Bacon <gbacon@hiwaay.net> wrote:
> In article <776e0325.0311260712.7b0c39cf@posting.google.com >,
> Sara <genericax@hotmail.com> 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:
>
> (?: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

--
"If a book is worth reading when you are six, * ben@morrow.me.uk
it is worth reading when you are sixty." - C.S.Lewis


All times are GMT. The time now is 01:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57