Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Removing an element from an array

by Anonymous Monk
on Jan 04, 2000 at 22:59 UTC ( #1696=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to completely remove an element from the center of an array. Are there any easy solutions to this problem?

Replies are listed 'Best First'.
Two Other Ideas
by chromatic (Archbishop) on Jan 23, 2000 at 09:59 UTC
    1) map the array elements to a hash, delete the key of the element you want to remove and map them back to an array.
    my @array = ("zero", "one", "two", "three", "four"); my %hash = map { $_ => "1" } @array; delete $hash{"two"}; @array = keys %hash; print "@array\n";
    Note that you won't get the keys out of a hash in the order you put them in unless you use a tied hash.

    2) If you know the array index, use array slices.

    my @array = ("zero", "one", "two", "three", "four"); my $remove = 2; # let's remove 2! @array = (@array[0 .. ($remove - 1)], @array[($remove + 1) .. $#array] +); print "\n@array\n"; print $array[0], "\n", $array[3];
Re: Removing an element from an array
by Anonymous Monk on Jan 04, 2000 at 23:41 UTC
    Actually, unless it is the last element, you might have to use 'splice' using null doesn't seem to work, and using undef doesn't remove the array element. Consider the following code and output:
    #!/usr/bin/perl my @array = ( "I", "am", "not", "a", "crook" ); print "Here is the original array\n"; my $ctr = 1; foreach( @array ) { print "$_\t $ctr\n"; $ctr++; } print "\n\nNow null it!\n"; $array[2] = NULL; $ctr = 1; foreach( @array ) { print "$_\t $ctr\n"; $ctr++; } print "\n\nNow undef it!\n"; $array[2] = undef; $ctr = 1; foreach( @array ) { print "$_\t $ctr\n"; $ctr++; } print "\n\nNow splice it!\n"; splice(@array, 2, 1); $ctr = 1; foreach( @array ) { print "$_\t $ctr\n"; $ctr++; }
    /tmp >./test
    Here is the original array
    I        1
    am       2
    not      3
    a        4
    crook    5
    Now null it!
    I        1
    am       2
    NULL     3
    a        4
    crook    5
    Now undef it!
    I        1
    am       2
    a        4
    crook    5
    Now splice it!
    I        1
    am       2
    a        3
    crook    4
Re: Removing an element from an array
by jamgold (Initiate) on Jan 15, 2000 at 23:20 UTC
    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";
      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); }

RE: Removing an element from an array
by Anonymous Monk on Jan 04, 2000 at 23:10 UTC

    Uhm... maybe:

    for ($i=$elementToRemove; $i<$#array;;){ $array[$i] = $array[++$i]; $array[$i] = NULL; }

    Now, check my syntax, this is a rough draft. Also, setting that element to NULL is an attempt to clear it. I'm hoping, but am not sure, that if you set the last element in an array to NULL, Perl will resize the array and free that memory. I don't imagine there are any problems with setting an element in the middle of the array to NULL.

    Could be, so check. I don't remember if $#array by itself will return the size or the last subscript or anything at all. I want the last subscript. Please, double check this.

    But your question was so short, and the answer seemed so obvious, that I think I'm missing something. Do you perhaps have a real issue that I'm not addressing? I'm not saying anything that can not be very easily put together with 2 minutes of browsing in even the worst of Perl manuals.


      Using null does not remove the array element. You might want to use the splice function to remove the required element. Shashidhar Iddamsetty

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1696]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2018-05-27 18:15 GMT
Find Nodes?
    Voting Booth?