Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Surprised by repetition (x) operator applied to a list

by BrowserUk (Pope)
on Mar 30, 2014 at 03:29 UTC ( #1080262=note: print w/ replies, xml ) Need Help??


in reply to Surprised by repetition (x) operator applied to a list

  1. This: ( [@list] ) x $nelts; says:

    Construct an anonymous array with copies of @list as its content; and then replicate the reference to it $nelts times.

  2. This: map { [@list] } 1 .. $nelts; says:

    Construct $nelts (different) anonymous arrays, that each contain copies of @list.

How would you do it?

I generally omit the block for that type of expression:

my @arr = map[ @list ], 1 .. $nelts;

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Surprised by repetition (x) operator applied to a list
Select or Download Code
Re^2: Surprised by repetition (x) operator applied to a list
by Athanasius (Monsignor) on Mar 30, 2014 at 03:55 UTC

    One way to demonstrate the difference is to replace the expression [@list] with an equivalent subroutine call:

    — which confirms that with map the sub is called three times, but with x it is called only once.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-09-21 12:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls