Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^2: How to remove the certain element of an object

by vagabonding electron (Chaplain)
on Feb 04, 2013 at 16:23 UTC ( #1016969=note: print w/replies, xml ) Need Help??

in reply to Re: How to remove the certain element of an object
in thread How to remove the certain element of an object

Dear sundialsvc4

thank you! Of course you are right. If the search element is not in the array then $idx is -1 and the subroutine just removes the last element of the array.

A quick solution could be to add the line

return @{$_[0]} if $idx == -1;

above "splice"

Here is a version which should be more readable:

sub purge_this_one { my $aref = $_[0]; my $searched = $_[1]; my $idx = first_index { $_ == $searched } @$aref; return @$aref if $idx == -1; splice( @$aref, $idx, 1 ); return @$aref; }

Thanks again!

Update:Well, it is not enough since the subroutine does not tell if it does not purge anything. It should warn at least. I take it home and try to make it better. Thank you for noticing this. Update:Removed some private lyric from the original text.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1016969]
and God said, "Let Newton be!"...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-01-23 14:37 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (248 votes). Check out past polls.