Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Surprised by repetition (x) operator applied to a list

by eyepopslikeamosquito (Archbishop)
on Mar 30, 2014 at 02:22 UTC ( [id://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?

Replies are listed 'Best First'.
Re: Surprised by repetition (x) operator applied to a list
by BrowserUk (Patriarch) 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,

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 :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found