Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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;

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

"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

In reply to Re: popping, shifting or splicing an array??? by injunjoel
in thread popping, shifting or splicing an array??? by drock

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (6)
    As of 2019-11-17 22:38 GMT
    Find Nodes?
      Voting Booth?
      Strict and warnings: which comes first?

      Results (87 votes). Check out past polls.