When you call left_triangle with what we call a List of List (LoL), you are passing a list of references to the sub, the reference to list(s) (3), (7,5), (2,4,6), (8,5,9,3). In Perl [] means reference to or address of or pointer to, basically the same idea. my @xx = ([3],[7,5],[2,4,6],[8,5,9,3]); is a list of 4 references to other lists. If you use that pointer to the list, then you are modifying the list itself.

Perl is MAGIC with lists!
How do you clone a LoL (List of List)? This is also called a "deep copy".
Easy:
my @xx_clone = map {[$_]}@xx;
Correction: my @xx_clone = map {[@$_]}@xx;
map is a transformation operator that operates on lists.
Here a list of references to lists (@xx) goes into the map. Then each list gets expanded via @$_, then enclosed within a new address (the [] operator) and a new list of those is passed to @xx_clone. So we have a new List of Lists! One line!

To do this in other languages like C, requires a lot of code! And if we are talking about "ragged arrays" like we have here with varying numbers of columns, even more code!


In reply to Re: Pass by Value does not work by Marshall
in thread Pass by Value does not work by Anonymous Monk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.