[VPM] testing two refs for same target

Darren Duncan darren at DarrenDuncan.net
Sat Jul 5 00:20:44 CDT 2003

At 8:10 PM -0700 7/4/03, Michael S. Joyce wrote:
> > In other words, does Perl have a built-in function like "remove_current_item"?  Currently I am doing something like this:
>> foreach my $i (0..$#items) {
>>	my $i_to_remove;
>>	if( $items[$i] eq $wanted_item ) {
>>		$i_to_remove = $i;
>>		last;
>>	}
>> }
>> splice( @items, $i_to_remove, 1 );
>This is the safe way to do it. You could also use grep.
>	@list = grep {$_ ne $wanted_item}, @list
>but be warned: that will remove everything that is equal to
>$wanted_item, not just the first one as yours does.

Hello Michael, thanks for your answers.  In the above example, it does look like your solution is much shorter than mine, and that it would work.  In some ways, your solution is better, and I will probably use it for that.  This is because (although I would have to verify it), a user may declare more than once that one node is the child of another, meaning a reference to the child would appear in the list of the parent's children more than once.  If I was severing the link (which I was), then I would actually want to remove all occurances.  One reason that I used the solution I did, aside from not thinking of the one you provided, is that it may be faster, since it doesn't go through the whole list every time.  On the other hand, maybe it isn't faster.  But I think I may go with your solution anyway. -- Darren Duncan

More information about the Victoria-pm mailing list