![]() |
aternative grouping and map, deuglification
I have a file like
CAT 4 DOG 3 CAT 6 CAT 9 BIRD 4 DOG 13 MOUSE 2 |
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 |
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 |
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@@ |
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 |
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/ |
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.