Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > map and grep again...

Reply
Thread Tools

map and grep again...

 
 
Marc Girod
Guest
Posts: n/a
 
      07-16-2010
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
 
Reply With Quote
 
 
 
 
sreservoir
Guest
Posts: n/a
 
      07-16-2010
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."
 
Reply With Quote
 
 
 
 
Marc Girod
Guest
Posts: n/a
 
      07-16-2010
On Jul 16, 4:05*pm, sreservoir <(E-Mail Removed)> wrote:

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


Thanks.

Marc
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-16-2010
On Fri, 16 Jul 2010 07:50:10 -0700 (PDT), Marc Girod <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Problems with std::map.insert() and std::map.find() Thomas J. Gritzan C++ 6 12-22-2008 10:01 AM
Re: Problems with std::map.insert() and std::map.find() acehreli@gmail.com C++ 1 12-21-2008 06:49 AM
STL map or hash map using struct as data and find it kl C++ 7 01-01-2008 11:05 AM
writing a function like map, sort, grep John Perl Misc 4 11-02-2004 11:17 PM
A Grep and a couple Awks <and a lot of Tassilo help> Agrapha Perl Misc 15 02-09-2004 08:24 PM



Advertisments