in reply to deleting array elements(efficiency/critique)

The main drawback I see with your code is that it copies elements from past the end of the array.

Here's a solution where the source index is the loop variable, and the destination follows behind it:

my $p = 0; for (my $q = 0; $q < @{$mon_obj->{'FILE_DATA'}}; ++$q) { if (defined $mon_obj->{'FILE_DATA'}->[$q]) { $mon_obj->{'FILE_DATA'}->[$p] = $mon_obj->{'FILE_DATA'}->[$q] if $p != $q; $p++; } } splice @{$mon_obj->{'FILE_DATA'}}, $p;

Replies are listed 'Best First'.
Re: Re: deleting array elements(efficiency/critique)
by blueAdept (Beadle) on Dec 27, 2000 at 23:14 UTC
    chipmunk-
    Nice re-write of my code..tad more thought involved to understand it, but it is quite beautiful once it clicks in the head.

    Although I think I'll end up using something like the replies that used grep, since it seems a shorter/clearer/rather efficient means of doing it.