in reply to Array splice

Well, if you just say

my @array; $array[12] = 5;

Perl silently assumes that you mean what you say and allocates sufficient memory to satisfy your wishes. (This has the potential downside that it allocates lots of memory and copies lots of data around if you are not careful.)

Why do you refer to strcpy in your example? As far as I remember this copies strings and not arrays in C?

Replies are listed 'Best First'.
Re^2: Array splice
by AnomalousMonk (Archbishop) on Aug 30, 2013 at 16:28 UTC

    Just to illustrate hdb's point that Perl's arrays are dynamic — and also that Perl supports negative indexing, which C doesn't like (other than a negative index of -1 for some reason, IIRC).

    >perl -wMstrict -le "my @array; print 'elements in array: ', scalar @array; ;; $array[1_000_000] = 42; print 'elements in array: ', scalar @array; ;; $array[-2] = 41;; print qq{elements at end of array: @array[ -2 .. -1 ]}; " elements in array: 0 elements in array: 1000001 elements at end of array: 41 42

    Update: Changed example code.

Re^2: Array splice
by code-ninja (Scribe) on Aug 30, 2013 at 07:27 UTC
    I refer to strcpy because it was kinda analogous to what I wanted to ask that's all.