http://qs1969.pair.com?node_id=482368


in reply to popping, shifting or splicing an array???

Greetings all,
Sounds like a job for grep to me.
Going with the
as numbers are selected remove elements from array A repopulate list with what is remaining into array A print array A
#!/usr/bin/perl -w use strict; use Dumpvalue; my $d = new Dumpvalue; #testing my @ArrayA = (0 .. 100); print "before\n"; $d->dumpValues(\@ArrayA); print "\n"; #more testing. my @numbers_to_remove = (14 .. 36, 50 .. 63, 89, 91, 94); #here we reassign @ArrayA with the return value from #our do{} block. @ArrayA = do { #make a local copy of @ArrayA as is to begin with my @tmp = @ArrayA; #map returns a list which comes in handy here map{ #make a local copy of the incoming elements from #our local copy in @tmp my $key = $_; #use a ternary operator to make sure we are #not including numbers from our #@numbers_to_remove list. If it is not #found return it otherwise return an #empty list. (!grep /^$key$/, @numbers_to_remove) ? $key : (); }@tmp; #for each element in @tmp }; print "After\n"; $d->dumpValues(\@ArrayA); print "\n";

Seems to work fine for me but I have not benchmarked this code.
Is this what you are looking for?

After re-reading the post I would suggest going with an Array of hashes depending on what is contained in the elements of @ArrayA. Hypothetically lets say you have a hash on information returned from a database query and you store a reference to each hash in the array @ArrayB. Then let the user select which elements to remove from the array based on some id.
use Dumpvalue; my $d = new Dumpvalue; #create the hypothetical structure. An array of #hash references each with an id field in it. my @ArrayB = map{{id=>$_}}(0 .. 100); $d->dumpValues(\@ArrayB); print "\n"; #more testing. #these would correspond to id's to remove in our hypothetical #scenario. my @ids_to_remove = (14 .. 36, 50 .. 63, 89, 91, 94); @ArrayB = do{ my @tmp = @ArrayB; map{ my $key = $_->{id}; (!grep /^$key$/, @ids_to_remove) ? $_ : () ; }@tmp; }; $d->dumpValues(\@ArrayB); exit;

Update!
I added some comments to the first block of code to hopefully explain what is going on.


-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo