![]() |
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? |
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 |
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 |
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 |
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. |
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 |
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 |
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 |
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 |
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.