Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Remove elements from an array

by rementis (Beadle)
on Nov 23, 2004 at 17:53 UTC ( #409943=perlquestion: print w/replies, xml ) Need Help??

rementis has asked for the wisdom of the Perl Monks concerning the following question:

thanks all! awesome responses, this helped me quite a bit!

Replies are listed 'Best First'.
Re: Remove elements from an array
by ikegami (Patriarch) on Nov 23, 2004 at 17:56 UTC

    Use Perl's grep!
    @array = grep { index($_, 'certain case-sensitive string') == -1 } @array;
    or
    @array = grep { index(lc($_), lc('certain case-insensitive string')) == -1 } @array;
    or
    @array = grep { !/certain regexp/ } @array;

Re: Remove elements from an array
by revdiablo (Prior) on Nov 23, 2004 at 18:04 UTC

    As ikegami says, you should use grep. He gives a good example for excluding elements based on a regular expression, but you might also want to exclude elements based on string equality (which would be far faster, though a different algorithm would probably be even better):

    my @new = grep { $_ ne "certain string" } @old;

    Or, perhaps you want to search for a substring:

    my @new = grep { index($_, "certain string") == -1 } @old;

    Just trying to give you some more ideas of how you can use grep... HTH

    Update: no fair! ikegami updated his post in the time while I was posting this. Cheater. :-)

Re: Remove elements from an array
by dimar (Curate) on Nov 23, 2004 at 18:01 UTC

    Remove elements that do not match the RegEx /B.*?y/

    use strict; use warnings; my @aTemp = qw(Fred Betty Barney Wilma); @aTemp = grep{$_ =~ /B.*?y/} @aTemp; print join',',@aTemp;

    Remove elements that *do* match the RegEx /B.*?y/

    use strict; use warnings; my @aTemp = qw(Fred Betty Barney Wilma); @aTemp = grep{$_ !~ /B.*?y/} @aTemp; print join',',@aTemp;

    Note: The other examples in this thread that use 'index' are more preferable for what you asked ... but this is an alternate way in case you are curious.

    Moreover, you might want to check out Categorized Questions and Answers where you can find fast answers to basic questions like How do I completely remove an element from an array ... categorized Q&A is very useful.

Re: Remove elements from an array
by bibo (Pilgrim) on Nov 23, 2004 at 17:59 UTC
    Assign an empty string... $foo[1] = ();

    how's that? --bibo UPDATE: no that's really wrong. ignore....

      That doesn't remove any elements, it just undefines an element. (splice(@foo, $i, 1) removes element $i.) Also, you didn't address finding the elements that don't "contain a certain string".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://409943]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-07-04 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?