Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
my @toRemove; foreach my $item ( (@array1, @array2) ) { if ( $item =~ /$pattern/i ) { push(@toRemove, $item); } } foreach my $item ( @toRemove ) { @array1 = grep { $_ ne $item } @array1; }

This is ugly, but to make this approach work, use a hash and remove a loop with a grep.

my @toRemove = grep m/$pattern/i, @array1, @array2; my %toRemove = map { ($_ => 1) } @toRemove; @array1 = grep ! $toRemove{$_}, @array1;
and if you like one liners, or simplicity, that becomes
@array1 = grep ! m/$pattern/i, @array1;
BTW: What is @array2 doing? It is useless unless you include it in the final line too.

Now to your issue of fun things that happen when you edit an array that is used in the controlling loop: Yes, that can mess things up. That is part of the reason that you're better off avoiding loops when you're getting rid of data. List operators, like grep and map eliminate a lot of the need for explicit loops.

Assigning a value to @array1 won't force change its scope. That will do the equivalent of empty all items from the list, and then push the new ones on to the end. No new variables will be created or destroyed.

If you really like managing loops and arrays yourself, use for instead of foreach, keep an index, and use splice to remove stuff.

splice @array,7,1;
will remove the 7th element (remember to count from 0) from the array.

Hope this helps

-- doug

In reply to Re^2: Answer: How do I completely remove an element from an array? by doug
in thread How do I completely remove an element from an array? by vroom

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found