Note that they all take time linear in the size of the array. If you need to do a lot of insertions in a long list, you ought to review your algorithm, and see whether you really need to keep a sorted list. Perhaps a (balanced) tree will be better.

I considered something like that, but I don't think it will be worth the extra complexity in this situation. Insertions (and deletions) won't be happening anywhere near as often as ordered processing of the list, which is easiest with a sorted array like this.

I'm also curious as to how Perl does slices internally if the loop version and the slice version are more or less the same. I'd assumed that a slice becomes an anonymous list, with copies of the original values. Wouldn't that take more time to setup than just iterating through the existing one?

Thanks for the splice tip. I wasn't aware of it:)


In reply to Re: Re: Shifting an Array by kelan
in thread Shifting an Array by kelan

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.