Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

#! perl use strict; use warnings; use Data::Dumper; my @list = qw(abc def); my $nelts = 3; print "\nCase 1:\n"; my @array1 = (copy(\@list)) x $nelts; $array1[0][0] = 'xyz'; $array1[0][1] = '123'; print Dumper(\@array1); print "\nCase 2:\n"; my @array2 = map { copy(\@list) } 1 .. $nelts; $array2[0][0] = 'uvw'; $array2[0][1] = '456'; print Dumper(\@array2); sub copy { my ($list_ref) = @_; print "call copy($list_ref)\n"; my @clone = @$list_ref; return \@clone; }

output:

13:51 >perl 888_SoPW.pl Case 1: call copy(ARRAY(0x1c7dec8)) $VAR1 = [ [ 'xyz', '123' ], $VAR1->[0], $VAR1->[0] ]; Case 2: call copy(ARRAY(0x1c7dec8)) call copy(ARRAY(0x1c7dec8)) call copy(ARRAY(0x1c7dec8)) $VAR1 = [ [ 'uvw', '456' ], [ 'abc', 'def' ], [ 'abc', 'def' ] ]; 13:51 >

— 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,


In reply to Re^2: Surprised by repetition (x) operator applied to a list by Athanasius
in thread Surprised by repetition (x) operator applied to a list by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2014-08-27 11:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (237 votes), past polls