aternative grouping and map, deuglification

Discussion in 'Perl Misc' started by Sara, Nov 25, 2003.

  1. Sara

    Sara Guest

    I have a file like

    CAT 4
    DOG 3
    CAT 6
    CAT 9
    BIRD 4
    DOG 13
    MOUSE 2
     
    Sara, Nov 25, 2003
    #1
    1. Advertising

  2. Sara

    Greg Bacon Guest

    In article <>,
    Sara <> 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
     
    Greg Bacon, Nov 25, 2003
    #2
    1. Advertising

  3. Sara

    Sara Guest

    (Greg Bacon) wrote in message news:<>...
    > In article <>,
    > Sara <> 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
     
    Sara, Nov 25, 2003
    #3
  4. 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:eek:^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@@
     
    Paul van Eldijk, Nov 26, 2003
    #4
  5. Sara

    Sara Guest

    "Paul van Eldijk" <> wrote in message news:<>...
    > 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
     
    Sara, Nov 26, 2003
    #5
  6. Sara

    Greg Bacon Guest

    In article <>,
    Sara <> 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/
     
    Greg Bacon, Nov 27, 2003
    #6
  7. Sara

    Ben Morrow Guest

    Greg Bacon <> wrote:
    > In article <>,
    > Sara <> 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, *
    it is worth reading when you are sixty." - C.S.Lewis
     
    Ben Morrow, Nov 27, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kl
    Replies:
    7
    Views:
    1,307
    James Kanze
    Jan 1, 2008
  2. Replies:
    1
    Views:
    435
    red floyd
    Dec 21, 2008
  3. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,033
    James Kanze
    Dec 22, 2008
  4. James Kanze
    Replies:
    0
    Views:
    2,026
    James Kanze
    Dec 21, 2008
  5. Kai-Uwe Bux
    Replies:
    1
    Views:
    1,159
    Kai-Uwe Bux
    Dec 21, 2008
Loading...

Share This Page