Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Expression form of map or grep

by ForgotPasswordAgain (Priest)
on Jul 12, 2020 at 00:03 UTC ( #11119190=note: print w/replies, xml ) Need Help??


in reply to Expression form of map or grep

FWIW, I agree with everything you said. However, I think you made a little error:

my @list = map( "$_ beads", @colors ), @grey_scale;

I think @list will not contain anything from @grey_scale, and would warn with something like "Useless use of private array in void context", because the assignment precedes the comma operator.

I do prefer the expression form with parentheses, though, partly because of an (I believe) obsolete reason which was that the block form would generate a new lexical space whereas the expression form wouldn't, so the expression form was apparently slightly more performant (on very large lists).

I also like making hashes with something like...

my %h = map +($_ => 1), @a;

...I guess because it seems the most generally correct, given the possible parsing bugs with Perl and map, but at the same time I can see that it's possibly confusing, especially given the "array in void context" comment above. :)

Replies are listed 'Best First'.
Re^2: Expression form of map or grep
by AnomalousMonk (Bishop) on Jul 12, 2020 at 01:56 UTC
    I think @list will not contain anything from @grey_scale, and would warn with something like "Useless use of private array in void context" ...

    Ah, good catch!

    c:\@Work\Perl\monks\Lady_Aleena>perl -wMstrict -MData::Dump -le "my @colors = qw(a b c); my @grey_scale = qw(x y); my @list = map( qq{$_ beads}, @colors ), @grey_scale; dd \@list; " Useless use of private array in void context at -e line 1. ["a beads", "b beads", "c beads"]
    Another set of parentheses is needed to include the second (and any subsequent) array:
    c:\@Work\Perl\monks\Lady_Aleena>perl -wMstrict -MData::Dump -le "my @colors = qw(a b c); my @grey_scale = qw(x y); my @list = (map( qq{$_ beads}, @colors ), @grey_scale); dd \@list; " ["a beads", "b beads", "c beads", "x", "y"]


    Give a man a fish:  <%-{-{-{-<

Re^2: Expression form of map or grep
by Lady_Aleena (Curate) on Jul 12, 2020 at 12:19 UTC

    It was my intent for @grey_scale to be part of @list, but not have "beads" mapped to it. I have a few modules that are lists starting with or have a map in the middle of the list, here is an example:

    My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena

      In the code as shown, "map" just melds in the array reference construction. Perhaps that is what you prefer.

      I personally would either make a temporary variable to hold the map result; or rearrange the code to highlight map function. On the second note ...

      'troll' => [ 'troll', map( "$_ troll", qw(desert freshwater giant ice saltwater snow spectr +al), 'two-headed' ), 'trobold', @$tralg, @$throglin ]

        I could spread it out, but there are almost 100 lists like it in the module. That is almost 100 lines Perl::Critic does not like too.

        My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

        No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
        Lady Aleena

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2020-10-25 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (248 votes). Check out past polls.

    Notices?