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