Home > Archive > PERL Miscellaneous > September 2005 > Can explain the MAP function more clearly? Thanks
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Can explain the MAP function more clearly? Thanks
|
|
|
| Hi :
I am with the sentence "
%seq_nos_set=map{$_=> 1} @seq_nos
Thanks for any information.
what is {$_=> 1 } mean?
| |
| A. Sinan Unur 2005-09-27, 9:56 pm |
| "yezi" <ye_line@hotmail.com> wrote in news:1127864918.739998.293400
@f14g2000cwb.googlegroups.com:
> Hi :
> I am 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/c...guidelines.html
| |
| Paul Lalli 2005-09-28, 8:06 am |
| yezi wrote:
> Hi :
> I am 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 2005-09-28, 8:06 am |
| 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 2005-09-28, 6:58 pm |
| 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 2005-09-28, 6:58 pm |
| "David K. Wall" <darkon.tdo@gmail.com> wrote in
news:Xns96DF6572239CAdkwwashere@216.168.3.30:
> A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
>
>
>
> @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/c...guidelines.html
| |
| Eric Schwartz 2005-09-28, 6:58 pm |
| Tad McClellan <tadmc@augustmail.com> writes:
> yezi <ye_line@hotmail.com> wrote:
>
>
> 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 2005-09-28, 6:58 pm |
|
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 2005-09-28, 6:58 pm |
| "T Beck" <Tracy.Beck@Infineon.com> writes:
> Paul Lalli wrote:
>
> 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 2005-09-28, 6:58 pm |
| T Beck wrote:
> Paul Lalli wrote:
>
> 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
| |
| xhoster@gmail.com 2005-09-28, 6:58 pm |
| Eric Schwartz <emschwar@pobox.com> wrote:
> "T Beck" <Tracy.Beck@Infineon.com> writes:
>
> 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.
I'm not sure what you mean about being ambiguous. Before perl
disambiguates the construct, it is always ambiguous. After it does so, it
is never ambiguous (but it might be wrong). The interesting thing is, it
guesses what you want before looking at the (lack of) comma.
map {$_=>1} @data;
block.
map {$_=>1}, @data;
block, syntax error (but if it disambiguated to hashref, then would not be
syntax error
map +{$_=>1}, @data;
hash ref.
map +{$_=>1} @data;
hash ref, syntax error
>
> -=Eric
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| T Beck 2005-09-28, 6:58 pm |
|
Paul Lalli wrote:
> T Beck wrote:
>
> Only when you (rather rudely, IMHO) snip the appropriate context.
Sorry about the rudeness, I snipped it that way because I thought you
were trying to draw attention to the {$_=>1} {without} the map in
front. Deconstructing the problem, as it were.. No offense intended.
> 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
Thanks for clearing that up. I had a feeling that I was missing
something, but every once in a while I get lucky and get something
right. This wasn't one of those times tho. Thanks to Xho and Eric for
their responses, as well. Quite insightful.
--T Beck
| |
| Eric Schwartz 2005-09-28, 6:58 pm |
| xhoster@gmail.com writes:
> Eric Schwartz <emschwar@pobox.com> wrote:
>
> I'm not sure what you mean about being ambiguous. Before perl
> disambiguates the construct, it is always ambiguous. After it does so, it
> is never ambiguous (but it might be wrong).
Well, I'd hope so, or it wouldn't be disambiguating, would it? :) I
meant that as far as I know, Perl will always decide it's one thing or
t'other; I don't know of any situation where Perl will give up and
say, "I don't know if I should treat this as a code block or a
hashref."
-=Eric
| |
| Heinrich Mislik 2005-09-29, 7:56 am |
| In article <Xns96DF6572239CAdkwwashere@216.168.3.30>, darkon.tdo@gmail.com says...
>
>
>A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
>
>
>
> @seq_nos_set{@seq_nos} = (1) x @seq_nos;
For me, this has two drawbacks:
1. It gets clumsy with strict:
my @seq_nos_set{@seq_nos} = (1) x @seq_nos;
gives Syntax error. One needs:
my %seq_nos_set;
@seq_nos_set{@seq_nos} = (1) x @seq_nos;
2. With a constant list you won't know the count:
@seq_nos_set{qw(foo bar hinz kunz)} = (1) x 100;
Any other elegant ways to create such hashes?
Greetings
Heinrich
--
Heinrich Mislik
Zentraler Informatikdienst der Universitaet Wien
A-1010 Wien, Universitaetsstrasse 7
Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
| |
| ced@carios2.ca.boeing.com 2005-09-29, 6:58 pm |
|
Heinrich Mislik wrote:
> In article <Xns96DF6572239CAdkwwashere@216.168.3.30>, darkon.tdo@gmail.com says...
>
> For me, this has two drawbacks:
>
> 1. It gets clumsy with strict:
>
> my @seq_nos_set{@seq_nos} = (1) x @seq_nos;
>
> gives Syntax error. One needs:
>
> my %seq_nos_set;
> @seq_nos_set{@seq_nos} = (1) x @seq_nos;
>
> 2. With a constant list you won't know the count:
>
> @seq_nos_set{qw(foo bar hinz kunz)} = (1) x 100;
>
> Any other elegant ways to create such hashes?
>
my %seq_nos_set = map { ($_, 1) } @seq_nos;
# my %seq_nos_set = map { ($_, 1) } qw/foo bar hinz kunz/;
--
Charles DeRykus
| |
| A. Sinan Unur 2005-09-29, 6:58 pm |
| NNTP-Posting-Host: pam-jy03p01.human.cornell.edu
X-Trace: ruby.cit.cornell.edu 1128022494 10650 128.253.251.245 (29 Sep 2005 19:34:54 GMT)
X-Complaints-To: newsmgr@cornell.edu
NNTP-Posting-Date: Thu, 29 Sep 2005 19:34:54 +0000 (UTC)
User-Agent: Xnews/5.04.25
Xref: number1.nntp.dca.giganews.com comp.lang.perl.misc:583443
"ced@carios2.ca.boeing.com" <ced@carios2.ca.boeing.com> wrote in
news:1128015098.460440.16800@o13g2000cwo.googlegroups.com:
>
> Heinrich Mislik wrote:
>
> my %seq_nos_set = map { ($_, 1) } @seq_nos;
Oh my, a perfect circle. Spooky!
Sinan
| |
| Bart Lateur 2005-09-29, 6:58 pm |
| yezi wrote:
>%seq_nos_set=map{$_=> 1} @seq_nos
>what is {$_=> 1 } mean?
It is a block of code that returns a list of two items:
($_, 1)
So for each item in the source list, this block is called, with $_ the
value of the current item, and this block returns a list with these two
values for each item. In the end, map will return a total list with 2
times as many items as the source list.
HTH,
Bart.
| |
| ced@carios2.ca.boeing.com 2005-09-29, 9:57 pm |
|
A. Sinan Unur wrote:
> "ced@carios2.ca.boeing.com" <ced@carios2.ca.boeing.com> wrote in
> news:1128015098.460440.16800@o13g2000cwo.googlegroups.com:
>
>
> Oh my, a perfect circle. Spooky!
Those who do not remember threads are doomed to repeat them...
--
Charles DeRykyus
| |
| Bart Lateur 2005-09-30, 3:56 am |
| Heinrich Mislik wrote:
>2. With a constant list you won't know the count:
>
>@seq_nos_set{qw(foo bar hinz kunz)} = (1) x 100;
>
>Any other elegant ways to create such hashes?
my %seq_nos_set;
$seq_nos_set{$_} = 1 foreach qw(foo bar hinz kunz);
--
Bart.
| |
| Bart Lateur 2005-09-30, 3:56 am |
| Eric Schwartz wrote:
>Well, I'd hope so, or it wouldn't be disambiguating, would it? :)
But Perl occasionally *is* ambiguous. Witness this silly example:
{ $_ => 1 };
Is this a block, or a hashref in void context?
--
Bart.
|
|
|
|
|