Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Re: Back to Remedial Perl for Me: map{} function

by mikfire (Deacon)
on Dec 15, 2000 at 20:34 UTC ( [id://46841]=note: print w/replies, xml ) Need Help??


in reply to Re: Back to Remedial Perl for Me: map{} function
in thread Back to Remedial Perl for Me: map{} function

Sorry, merlyn, but the second example *is* turning a list into another list. map is very capable of mapping 1-to-n relations, ie, every element on the right will produce more than one element on the right.

If I read your statement correctly ( and I may not have ), the first use of map isn't correct

my @first = qw/ one-1 two-2 three-3/; my %hash = map { split /-/ } @first; # Which works for me btw print map { "$_ => $hash{$_}\n" } keys %hash;
But this usage is correct
my @first = qw/ one-1 two-2 three-3/; my @second = map { split /-/ } @first; my %hash = @second; # Legal code, isn't it? print map { "$_ => $hash{$_}\n" } keys %hash;

How else is map supposed to make @first into @second except by performing an action on every element of @first?

I am not arguing that a foreach wouldn't be appropriate here. If that is what works, by all means use it. But if I am not supposed to use map when I want an action on every element of an array, doesn't it make more sense to say

@second = @first;
because that seems to be the only option you have left me with this statement.

Would you be kind enough to expand on your answer so I can figure out what I missed?

TIA
mikfire

Replies are listed 'Best First'.
Re: Re: Re: Back to Remedial Perl for Me: map{} function
by merlyn (Sage) on Dec 15, 2000 at 20:35 UTC
    I wasn't commenting on either of those. I was commenting on the original poster's last example:
    if ($access) { foreach (@{$access}) { $set{$_->[0]} = $_->[1]; } }
    That's just fine as it is. No need for a map. In fact, a map would be difficult here, unless we presume %set is already empty before we get here.

    -- Randal L. Schwartz, Perl hacker

      Ahh. I got scope locked and assumed that you were referring to the map examples, not the foreach loop.

      Thanks
      mikfire

Re: Re: Re: Back to Remedial Perl for Me: map{} function
by $code or die (Deacon) on Dec 15, 2000 at 21:05 UTC
    Out of interest, why does this (both examples) print:
    one => 1 three => 3 two => 2
    and not:
    one => 1 two => 2 three => 3

      Because hashes are unordered.

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

        "unsorted" would be more precise...the elements in the hash do have an order, but not not "in the order" that we might put them.
        That's what I thought, but it did the same order every time I ran it, so it seems that there is some order (i.e. it's not completely random). Was just wondering how perl decides to order it. Even Data::Dumping the hash gives the same order!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://46841]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found