in reply to Re^3: delete array element from array ref
in thread delete array element from array ref

While I don't share llancet's concerns and attitude nor do I understand his/her "hate" (such a strong word...) for splice, I must admit that if I think of it, its interface is not that handy. Generally one doesn't realize, because... well: as a matter of a fact we don't need that often to remove random elements from the middle of an array(-ref) I don't, at least; in fact I can't remember the last time did I had such a requirement. Probably my mindset is so formed as to devise algorithms that will make me choose an alternative path anyway.

Said all this, we have array subscripting. Perhaps one could like a more "agressive" form of subscripting that at the same time would remove the elements from the wanted array. Of course all good subscripting symbols are already gone, and moreover a similar interface would be either clumsy or risky. Perhaps, given that one suitable function to do what the OP wants could (not claiming it would be the best one in any way!) be

sub extract { my $aref=shift; my @out=@{$aref}[@_]; @$aref=@{$aref}[grep !($_ ~~ @_) => 0..$#$aref]; @out; }

a convenient syntax from the UI POV could be given by autobox, i.e. the following should work - but it is untested as ATM I don't have a Perl installation with that module installed and working:

#!/usr/bin/perl use strict; use warnings; use 5.010; use autobox; use Data::Dumper; sub ARRAY::extract { my $aref=shift; my @out=@{$aref}[@_]; @$aref=@{$aref}[grep !($_ ~~ @_) => 0..$#$aref]; @out; } my @x0 = my @x = 'a'..'g'; my @y = @x->extract(2,3,-8); print Dumper \(@x0, @y, @x); __END__

AIUI, it could even be made into a package like thus:

# -*- Perl -*- use strict; use warnings; use 5.010; package Array::Extract; use base qw/autobox/; sub import { my $class = shift; $class->SUPER::import(ARRAY => 'Array::Extract::Work'); } package Array::Extract::Work; sub extract { my $aref=shift; my @out=@{$aref}[@_]; @$aref=@{$aref}[grep !($_ ~~ @_) => 0..$#$aref]; @out; } 1; __END__

Then one could say:

use Array::Extract; my @x = 'a'..'g'; my @y = @x->extract(2,3,-8);

Incidentally, my eternal gratitude to whoever will give me a working ppm for autobox under 5.10.*... Update: [Thu Aug 28 10:14:09 2008] problem solved, jand has my eternal gratitude!

--
If you can't understand the incipit, then please check the IPB Campaign.