map and grep again...

Discussion in 'Perl Misc' started by Marc Girod, Jul 16, 2010.

  1. Marc Girod

    Marc Girod Guest

    Hello,

    I know this has been explained earlier... but why are map and grep
    behaving differently here?

    #!/usr/bin/perl -w

    use strict;
    use feature qw(say);

    my @glb = qw(lbtype:FFF);
    say $_ for map { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;
    @glb = qw(lbtype:FFF);
    say $_ for grep { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;

    Running this gives:

    $ /tmp/foo
    1
    FFF

    Thanks,
    Marc
     
    Marc Girod, Jul 16, 2010
    #1
    1. Advertising

  2. Marc Girod

    sreservoir Guest

    On 7/16/2010 10:50 AM, Marc Girod wrote:
    > Hello,
    >
    > I know this has been explained earlier... but why are map and grep
    > behaving differently here?
    >
    > #!/usr/bin/perl -w
    >
    > use strict;
    > use feature qw(say);
    >
    > my @glb = qw(lbtype:FFF);
    > say $_ for map { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;


    map collect return values.

    > @glb = qw(lbtype:FFF);
    > say $_ for grep { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;


    grep collects when return value is true.

    > Running this gives:
    >
    > $ /tmp/foo
    > 1
    > FFF


    for (@glb) { s///; say }

    of course. s/// return 1 or !1, depending whether something was matched
    or replaced.

    please don't use map and grep purely for side-effects unless you know
    what you're doing.

    and you don't have to specify the implicit argument.

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe."
     
    sreservoir, Jul 16, 2010
    #2
    1. Advertising

  3. Marc Girod

    Marc Girod Guest

    On Jul 16, 4:05 pm, sreservoir <> wrote:

    > map collect return values.
    > grep collects when return value is true.


    Thanks.

    Marc
     
    Marc Girod, Jul 16, 2010
    #3
  4. Marc Girod

    Guest

    On Fri, 16 Jul 2010 07:50:10 -0700 (PDT), Marc Girod <> wrote:

    >Hello,
    >
    >I know this has been explained earlier... but why are map and grep
    >behaving differently here?
    >
    >#!/usr/bin/perl -w
    >
    >use strict;
    >use feature qw(say);
    >
    >my @glb = qw(lbtype:FFF);
    >say $_ for map { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;
    >@glb = qw(lbtype:FFF);
    >say $_ for grep { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;
    >
    >Running this gives:
    >
    >$ /tmp/foo
    >1
    >FFF
    >
    >Thanks,
    >Marc


    map {} .. is going to give you the result of the operation.
    The line with map is creating a list with the result of the
    regular expression.
    Grep is is creating a list of sucessfull matches.

    You confuse the issue when you when you stick map or grep
    inside the for expression then put the default $_ inside its
    body. Obviously each is say(ing) the itterative list values
    AFTER the list is created.
    So,
    say $_ for map { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;
    ^^ is NOT { $_ =~ s/^lbtype:(.*)(\@.*)?$/$1/ }
    ^^

    Nothing wrong with clarity.

    print "----\n";
    @glb = qw(lbtype:FFF);
    @list = map { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;
    for (@list) {
    say $_;
    }
    @glb = qw(lbtype:FFF);
    @list = map { s/^lbtype:(.*)(\@.*)?$/$1/; $_ } @glb;
    for (@list) {
    say $_;
    }
    print "----\n";
    @glb = qw(lbtype:FFF);
    @list = grep { s/^lbtype:(.*)(\@.*)?$/$1/ } @glb;
    for (@list) {
    say $_;
    }

    -sln
     
    , Jul 16, 2010
    #4
    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. amit
    Replies:
    0
    Views:
    394
  2. che
    Replies:
    2
    Views:
    504
  3. abcd

    Importing again and again

    abcd, Jun 8, 2006, in forum: Python
    Replies:
    9
    Views:
    330
    Maric Michaud
    Jun 9, 2006
  4. Ò»Ê×Ê«

    A design problem I met again and again.

    Ò»Ê×Ê«, Apr 1, 2009, in forum: Python
    Replies:
    24
    Views:
    731
    Dennis Lee Bieber
    Apr 4, 2009
  5. John
    Replies:
    4
    Views:
    126
    Ben Morrow
    Nov 2, 2004
Loading...

Share This Page