Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Keep It Simple, Stupid
 
PerlMonks  

Re: How do I delete a row of an array

by ChrisR (Hermit)
on Oct 09, 2003 at 10:48 UTC ( [id://297929]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to How do I delete a row of an array

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?

Replies are listed 'Best First'.
Re: Re: How do I delete a row of an array
by vek (Prior) on Oct 09, 2003 at 11:45 UTC

    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 --

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://297929]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.