in reply to Removing an element from an array

I bet there is a more elegant way to do this (hey, after all this is perl and there are a 1000 ways to skin a ...) but this was the quickest thing I could come up with.

I found out that I have to introduce a temporary variable, because the first splice modifies @a

sub rm ($@) { my ($e,@a) = @_; my @t = @a; (splice(@a,0,--$e),splice(@t,++$e)); } my @array = (1..10); # # remove the 5th Element # @array = &rm(5,@array); print join ("\n",@array) . "\n";

Replies are listed 'Best First'.
Re: Removing an element from an array
by Anonymous Monk on Apr 26, 2001 at 15:36 UTC
    Is there a good reason not to simplify it like this ? As you already mentioned, the first splice modifies @a ...

    sub rm($@) { my ($e,@a) = @_; splice(@a,$e,1); return (@a); }

    Torsten