Using the "x" operator in a list context generates a list that seems to be optimized at assignment such that the first element is properly generated, and subsequent elements are simply references to the first.

use Data::Dumper; my @x; push @x, ({a=>3,b=>4}) x 5 ; $x[3]->{b}=9; # I want to modify the Fourth element only print Dumper ( \@x);" -- Output -- $VAR1 = [ { 'a' => 3, 'b' => 9 }, $VAR1->[0], $VAR1->[0], $VAR1->[0], $VAR1->[0] ];
As you can see, it does NOT do what I want - it modifies ALL elements (since they are references), not just a single one.

I have, in the past, got around this using map to generate individual elements, but, at this point, I am irritated that "x" will not comply, and I am seeking monk wisdom in subjugating it. At the very minimum, I'd like to understand the behaviour or history related to this implementation of the operator.

I have previously referred to this issue in a response to this node, with a fellow monk agreeing that it was an enigma, but no solution.

Probably irrelevant info: perl, v5.8.7 built for MSWin32-x86-multi-thread.

Update:Thanks merlyn. I guess that means that "x", by design/definition is not suited/usable for this purpose. Veni, Vedi, Vici is a fantasy. Sigh!.

     "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken


In reply to Conquering "x" operator over-optimization by NetWallah

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.