in reply to Removing null values from within an array

Wow, I surely didn't now it was this difficult to remove array elements. I thought deleting them actually removed it. But if you still haven't got it working, why not instead build a new array with just the good elements? You don't have to worry about deleting anything.
my @original = ("", undef, "perl hungry", "", "twenty"); my @original_kept; foreach(@original) { my $what_we_removed = shift(@original); # take our first item off and +keep it if (defined $what_we_removed && $what_we_removed ne "") { push(@original_kept, "$what_we_removed"); } } print join("\n", @orignal_kept);


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Re: Removing null values from within an array
by TomDLux (Vicar) on Mar 07, 2004 at 03:45 UTC

    Removing array elements is easy.

    If you want to remove the value without losing the slot, use delete. If you want to eleminate the slot and value together, use splice:

    @a = qw( 0 1 2 3 4 5 6 ) # starting at position 2, delete 1 element splice @a, 2, 1 print join ", ", @a # generates the output : 0, 1, 3, 4, 5, 6

    splice() can do lots more ...see perldoc -f splice

    --
    TTTATCGGTCGTTATATAGATGTTTGCA