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.

In reply to Re^4: delete array element from array ref by blazar
in thread delete array element from array ref by llancet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.