perl arrays keep a "start-of-array" number in their datastructures, so using shift() usually doesn't move any items at all and it's generally pretty efficient to shift() even very large arrays.

shift never moves any items, and thus it's extremely efficient no matter the size of the array.

@a = qw( a b c ); +---+---+---+---+ @a = | a | b | c | X | ( X = spare ) +---+---+---+---+ ^ ^ | | start end shift @a; +---+---+---+---+ @a = | X | b | c | X | +---+---+---+---+ ^ ^

I'm assuming shift/push combos move stuff around every once in a while, since I write code that depends on that behaviour and it seems to work, but I'm only 90% certain on that.

It's not the shift/push combo, it's push alone that causes the moving.

For efficiency, arrays can have more elements allocated than necessary. If a push is performed, these spare elements will be used. If a push is performed and there no spare elements, a new bigger array is allocated and the elements (pointers) are moved to the new array. This occurs whether shift was used or not.

[ continuing from above ] push @a, 'd'; +---+---+---+---+ @a = | X | b | c | d | +---+---+---+---+ ^ ^ push @a, 'e'; -> No more space! Re-allocation occurs. -> $new_buf_size = $old_buf_size * 2 + 4 -> Pointers to elements are quickly copied to newly allocated buffer. +---+---+---+---+ | X | b | c | d | +---+---+---+---+ / / / / / / / / / v v v +---+---+---+---+---+---+---+---+---+---+---+---+ @a = | b | c | d | e | X | X | X | X | X | X | X | X | +---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^

In reply to Re^2: shift v. indexing into an array by ikegami
in thread shift v. indexing into an array by 7stud

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.