Your example demonstrates an off-by-one error: $array[7] contained 8 to begin with, and afterward, would contain 89.

This is one of those things that you could easily try yourself. If you have Perl installed, you would do this:

my @array = ( 1 .. 10 ); $array[7] = $array[7] . $array[8]; print "$array[7]\n"; print "$array[8]\n";

And you will see the following output:

89 9

...which would tell you that $array[8] hasn't been modified in any way. As a matter of fact, it would be a really difficult to use language if something as innocuous as the concatenation operator could modify its operands (not counting stringification). Imagine this:

my @array = ( 1 .. 10 ); $array[7] = $array[7] + $array[8]; print "$array[7]\n"; print "$array[8]\n";

There, you would expect to see:

17 9

And you would complain if simple addition caused one of the operands to vaporize. Concatenation is not so different, in this regard.

Now if it is your intention to vaporize an element in an array and shift everything past that point up to fill the void, look at splice, but be forewarned that shifting elements to lower indices is an O(n) operation, meaning that it consumes as much computational time as it takes to shift one element, then to shift the next one, then to shift the next one, and so on, until done -- as the array grows, the time grows linearly.


Dave


In reply to Re: Concatenating Array elements by davido
in thread Concatenating Array elements by lmtaylor

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.