Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Can explain the MAP function more clearly? Thanks (http://www.velocityreviews.com/forums/t894446-can-explain-the-map-function-more-clearly-thanks.html)

yezi 09-27-2005 11:48 PM

Can explain the MAP function more clearly? Thanks
 
Hi :
I am confused with the sentence "

%seq_nos_set=map{$_=> 1} @seq_nos

Thanks for any information.

what is {$_=> 1 } mean?


A. Sinan Unur 09-28-2005 12:04 AM

Re: Can explain the MAP function more clearly? Thanks
 
"yezi" <ye_line@hotmail.com> wrote in news:1127864918.739998.293400
@f14g2000cwb.googlegroups.com:

> Hi :
> I am confused with the sentence "
>
> %seq_nos_set=map{$_=> 1} @seq_nos
>
> Thanks for any information.
>
> what is {$_=> 1 } mean?


Have you read the documentation?

From perldoc -f map:

%hash = map { getkey($_) => $_ } @array;

is just a funny way to write

%hash = ();
foreach $_ (@array) {
$hash{getkey($_)} = $_;
}

so

my %seq_nos_set = map{$_ => 1} @seq_nos;

is another way of writing:

my %seq_nos_set;
$seq_nos_set{$_} = 1 for @seq_nos;

Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Paul Lalli 09-28-2005 11:45 AM

Re: Can explain the MAP function more clearly? Thanks
 
yezi wrote:
> Hi :
> I am confused with the sentence "
>
> %seq_nos_set=map{$_=> 1} @seq_nos
>
> Thanks for any information.
>
> what is {$_=> 1 } mean?


{ } indicates a block
=> is a fancy way of writing a comma

So { $_ => 1 } is a block that returns a 2-element list ($_, 1)

map() takes this block and evaluates it for each element of @seq_nos
(each time setting $_ to the current element of @seq_nos). Each
evaluation of the block is then added to %seq_nos_set. Therefore
%seq_nos_set will be a hash (which is a list of key/value pairs) where
all the keys are elements of @seq_nos, and all the values are 1.

Paul Lalli


Tad McClellan 09-28-2005 12:19 PM

Re: Can explain the MAP function more clearly? Thanks
 
yezi <ye_line@hotmail.com> wrote:


> what is {$_=> 1 } mean?



It means the same as

{$_, 1 }


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

David K. Wall 09-28-2005 01:58 PM

Re: Can explain the MAP function more clearly? Thanks
 
A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:

> my %seq_nos_set = map{$_ => 1} @seq_nos;
>
> is another way of writing:
>
> my %seq_nos_set;
> $seq_nos_set{$_} = 1 for @seq_nos;



@seq_nos_set{@seq_nos} = (1) x @seq_nos;

is the way I tend to write something like this. I'm not sure exactly
WHY I got into that habit, though.

A. Sinan Unur 09-28-2005 02:15 PM

Re: Can explain the MAP function more clearly? Thanks
 
"David K. Wall" <darkon.tdo@gmail.com> wrote in
news:Xns96DF6572239CAdkwwashere@216.168.3.30:

> A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
>
>> my %seq_nos_set = map{$_ => 1} @seq_nos;
>>
>> is another way of writing:
>>
>> my %seq_nos_set;
>> $seq_nos_set{$_} = 1 for @seq_nos;

>
>
> @seq_nos_set{@seq_nos} = (1) x @seq_nos;
>
> is the way I tend to write something like this. I'm not sure exactly
> WHY I got into that habit, though.


I find it very visually appealing. That might explain it :)

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Eric Schwartz 09-28-2005 03:39 PM

Re: Can explain the MAP function more clearly? Thanks
 
Tad McClellan <tadmc@augustmail.com> writes:
> yezi <ye_line@hotmail.com> wrote:
>> what is {$_=> 1 } mean?

>
>
> It means the same as
>
> {$_, 1 }


Every now and then I learn something new about Perl that is so basic.
I had mentally tagged => as "quote LHS, and then act as comma"; your
comment prompted me to look up perlop, which said,

If the argument on the left is not a word, it is first
interpreted as an expression, and then the string value
of that is used.

It's always nice when I can learn something new about something I
thought I understood already.

-=Eric

T Beck 09-28-2005 05:02 PM

Re: Can explain the MAP function more clearly? Thanks
 

Paul Lalli wrote:
> { } indicates a block
> => is a fancy way of writing a comma
>
> So { $_ => 1 } is a block that returns a 2-element list ($_, 1)



Forgive me if I'm wrong, but in this case, wouldn't {} refer to an
anonymous hash?

--T Beck


Eric Schwartz 09-28-2005 05:33 PM

Re: Can explain the MAP function more clearly? Thanks
 
"T Beck" <Tracy.Beck@Infineon.com> writes:
> Paul Lalli wrote:
>> { } indicates a block
>> => is a fancy way of writing a comma
>>
>> So { $_ => 1 } is a block that returns a 2-element list ($_, 1)

>
> Forgive me if I'm wrong, but in this case, wouldn't {} refer to an
> anonymous hash?


This is in the context of the map function, so you should look at

perldoc -f map

To verify your assumption is correct.

map BLOCK LIST
map EXPR,LIST

So map takes either a BLOCK or an EXPR, and in this case it's a block.

"Wait a second," I hear you saying. "Doesn't { ... } make a hashref?"

Yes, it does-- but it also makes a block. Consider this:

sub foo
{
print "Hi, mom!\n";
}

Do the { } there make a hashref? No. Why? Because Perl can usually
tell whether you want a block or not. Offhand I can't think of a case
where it's ambiguous, but I'm sure someone else can.

-=Eric

Paul Lalli 09-28-2005 05:39 PM

Re: Can explain the MAP function more clearly? Thanks
 
T Beck wrote:
> Paul Lalli wrote:
> > { } indicates a block
> > => is a fancy way of writing a comma
> >
> > So { $_ => 1 } is a block that returns a 2-element list ($_, 1)

>
> Forgive me if I'm wrong, but in this case, wouldn't {} refer to an
> anonymous hash?


Only when you (rather rudely, IMHO) snip the appropriate context.

my %hash = map { $_ => 1 } @foo;

{ $_ => 1 } is the block (or anonymous subroutine) that's passed as the
first argument to map. This block returns a two element list.

If the code was something along the lines of
my $hash_ref = { $_ => 1 };

then the braces would, indeed, create an anonymous hashref.

map()'s prototype, effectively, is:
sub map(&@);
which, perldoc perlsub tells us: "An & requires an anonymous
subroutine, which, if passed as the first argument, does not require
the sub keyword or a subsequent comma"

For more information:
perldoc -f map

Paul Lalli



All times are GMT. The time now is 03:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57