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

Re: array filter

by why_bird (Pilgrim)
on Nov 18, 2008 at 08:10 UTC ( #724217=note: print w/replies, xml ) Need Help??


in reply to array filter

First of all, try super search. There are lots of nodes out there that ask similar questions. Here is the one, for example that I picked off the top of the list.

Now, I'm sure there are better ways of doing this, but the simple implementation just iterates over both arrays and uses splice.

#! /usr/bin/perl use strict; use warnings; use Data::Dumper; my @array=qw(In to the wild); my @filterarray=qw(the); print Dumper @array; for(my $i=0;defined $array[$i];$i++){ my $word=$array[$i]; foreach my $filter (@filterarray){ if($filter eq $word){ splice (@array,$i,1); } } } print Dumper @array; exit 0;

In case you're not familiar, Data::Dumper is your friend when it comes to quickly printing out output, especially arrays and hashes. 'qw' means separate whatever's inside the brackets and put them in single quotes (no variable interpolation). So qw(Into the wild) -> ('Into', 'the', 'wild'). And always use strict and use warnings.

hope that helps
why_bird

........
Those are my principles. If you don't like them I have others.
-- Groucho Marx
.......

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2021-10-23 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (88 votes). Check out past polls.

    Notices?