Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Surprised by repetition (x) operator applied to a list

by eyepopslikeamosquito (Canon)
on Mar 30, 2014 at 02:22 UTC ( #1080254=perlquestion: print w/ replies, xml ) Need Help??
eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:

Running:

use strict; use warnings; my @list = ( "abc", "def" ); my $nelts = 3; # Case 1 { my @arr = ( [@list] ) x $nelts; for my $e (@arr) { print "Case 1 before: $e: $e->[0] $e->[1]\n" } $arr[0]->[0] = "xyz"; $arr[0]->[1] = "123"; for my $e (@arr) { print "Case 1 after : $e: $e->[0] $e->[1]\n" } } print "\n"; # Case 2 { my @arr = map { [@list] } 1 .. $nelts; for my $e (@arr) { print "Case 2 before: $e: $e->[0] $e->[1]\n" } $arr[0]->[0] = "xyz"; $arr[0]->[1] = "123"; for my $e (@arr) { print "Case 2 after: $e: $e->[0] $e->[1]\n" } }
produces:
Case 1 before: ARRAY(0x61ddc8): abc def Case 1 before: ARRAY(0x61ddc8): abc def Case 1 before: ARRAY(0x61ddc8): abc def Case 1 after : ARRAY(0x61ddc8): xyz 123 Case 1 after : ARRAY(0x61ddc8): xyz 123 Case 1 after : ARRAY(0x61ddc8): xyz 123 Case 2 before: ARRAY(0x61de28): abc def Case 2 before: ARRAY(0x315918): abc def Case 2 before: ARRAY(0x315990): abc def Case 2 after: ARRAY(0x61de28): xyz 123 Case 2 after: ARRAY(0x315918): abc def Case 2 after: ARRAY(0x315990): abc def
As you can see, Case 1 above produces a list of identical references (so that changing the value of the first item in the list results in all list values changing) while with Case 2 above, changing the value of the first item leaves the other list values unchanged. Now Case 2 is the behavior I wanted but I (foolishly) started with:
my @arr = ( [@list] ) x $nelts;
... got surprised when changing one list element changed them all, so randomly switched to:
my @arr = map { [@list] } 1 .. $nelts;
Since this is a fairly common operation, I was wondering if there is a "standard" way to do Case 2. How would you do it?

Comment on Surprised by repetition (x) operator applied to a list
Select or Download Code
Re: Surprised by repetition (x) operator applied to a list
by Anonymous Monk on Mar 30, 2014 at 02:42 UTC
    map is the standard way :) map introduces a new scope the repeat operator doesn't :)
Re: Surprised by repetition (x) operator applied to a list
by BrowserUk (Pope) on Mar 30, 2014 at 03:29 UTC
    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.

      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: perlquestion [id://1080254]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-12-29 07:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls