in reply to On Removing Elements from Arrays

The splice() function slides elements of the array around as necessary.

As far as question 2 goes, I'd use:

join "", map defined($_) ? $_ : "undef", @array;

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: On Removing Elements from Arrays
by danger (Priest) on Sep 23, 2001 at 06:00 UTC

    Then again, depending on what you want to test for you may now also test whether it is undefined because it was assigned an undefined value (explicitly or implicitly) or because it was deleted (or never explicitly used in an assignment):

    my @array; @array[3..9] = (3,4,undef,undef,7); delete @array[4,8]; print join"\n",map{defined $array[$_] ? "$_: $_" : exists $array[$_] ? "$_: undef-but-exists" : "$_: undef-not-exists"} 0 .. 10; __END__ OUTPUT: 0: undef-not-exists 1: undef-not-exists 2: undef-not-exists 3: 3 4: undef-not-exists 5: undef-but-exists 6: undef-but-exists 7: 7 8: undef-not-exists 9: undef-but-exists 10: undef-not-exists

    This output may seem confusing. 0-3 don't exist because they were never assigned anything. 4 and 8 don't exist because we deleted them. 5 and 6 exist because we explicitly assigned the undef value to them. Element 9 does exist because when you assign a smaller list to a larger slice, the remainder of the slice is implictly assigned undef values. 10 doesn't exist because we've never assigned anything to it (we are beyond the end of the array).