TwitchyEye has asked for the wisdom of the Perl Monks concerning the following question:

Hi, This seems like a simple question but my Perl for dummies book doesn't seem to cover it. How do I delete row $i from an array? Thanks, Twitchyeye

Replies are listed 'Best First'.
Re: How do I delete a row of an array (use splice, not delete)
by grinder (Bishop) on Oct 09, 2003 at 13:36 UTC

    Look for the splice function. It's like substr except it works on arrays.

    splice( @foo, $i, 1 ) is probably what you are looking for. Don't use delete, the behaviour depends on what version of Perl you are using.

      There is another important difference between splice and substr that should be mentioned. If you call
      substr EXPR,OFFSET,LENGTH substr EXPR,OFFSET
      This will not erase anything in the string while if you use
      splice ARRAY,OFFSET,LENGTH splice ARRAY,OFFSET
      You will erase elements from the array
        Thanks for all the responses.
        I am currently using the splice command in my script.
        It seems it is leaving blank data in row $i. When I view my table after using the splice command, the row $i data is replaced with blank data rather than being completely removed.
        I will try the delete function also to see if it works.
        Twitchy Eye
Re: How do I delete a row of an array
by nimdokk (Vicar) on Oct 09, 2003 at 13:20 UTC
    I'm not quite sure what you mean, an array is a list containing elements. Do you want to remove one particular element from a list? Or are you talking about an Array of Arrays and need to delete one Array from the Array?

    update: To remove an element from an array, take a look at the following code sample.

    my @rray=qw(mercury venus earth mars jupiter saturn neptune uranus plu +to); print "\@rray contains: @rray\n"; print "Removing $rray[1]\n"; @new_array=splice(@rray,1,1); print "\@new_array now contains: @new_array\n"; print "\@rray now contains: @rray\n";


    "Ex libris un peut de tout"
Re: How do I delete a row of an array
by snax (Hermit) on Oct 09, 2003 at 13:36 UTC
    Check out splice. It should do what you want.
Re: How do I delete a row of an array
by thens (Scribe) on Oct 09, 2003 at 13:48 UTC

    How do I delete row $i from an array

    If you have read the perldoc you wouldnt call the elements of an array as 'row'. In perl there are no multi-dimensional array. Having said that, there are ways by which you can create a multi-dimensional array in perl.

    see perldoc perldata for a complete discussion about the data structures supported in perl.

    -T

    use perl; use strict;

Re: How do I delete a row of an array
by ChrisR (Hermit) on Oct 09, 2003 at 14:48 UTC
    I could be wrong about this, very wrong considering the other answers to your post but here it is anyway. The delete function should take care of this for you. Here is an example:
    delete $array[$index];
    or
    delete $array[$index][$index2];
    This should remove the specified element from the array. If this is incorrect, I'm sorry for the confusion and would appreciate it if someone could explain why delete won't work for this.

    Update:
    According to the camel book, starting with perl version 5.6, this function will work for arrays whereas in previous versions it only worked with hashes. It also says: Deleting from an array causes the element at the specified position to revert to a completely uninitialized state, but it doesn't close up the gap. Is this the reason that delete shouldn't be used and if so what are the specific pitfalls in this?

      Is this the reason that delete shouldn't be used and if so what are the specific pitfalls in this?

      Well one reason would be that your code would break if you needed it to run on Perl < 5.6. You'd also have to take into account that the element itself does not get removed. A 10 element array is still a 10 element array only now one of the elements has an uninitialized value. If you take that into account in your code you'll be ok.

      for my $foo (@array) { next if (! $foo); }

      Personally I'd stick with splice as it's more DWIM - remove an element in the middle of an array and the index of the ones after it will now shift down.

      -- vek --