Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove elements from array 1

Status
Not open for further replies.

nychris

MIS
Dec 4, 2004
103
US
I have two arrays, they both include the names of animals.
Code:
@animals = qw/cat dog beaver frog elephant/;
@exclude = qw/beaver frog/;
How can I remove each element in @exclude from @animals? I want @animals to consist of "cat, dog, elephant" after the other elements are removed.

Thanks!

--
Chris
RHCE, SCSA, AIX5L, LPIC, CNE, CCNA, MCSE
 
one way:

Code:
@animals = qw/cat dog beaver frog elephant/;
@exclude = qw/beaver frog/;
%exclude =  map {$_,$_} @exclude;
@animals = grep {!exists($exclude{$_})} @animals; 
print "$_\n" for @animals;
 
very nice, thanks! I don't quite understand how it works though, I have to read more on the map function.

--
Chris
RHCE, SCSA, AIX5L, LPIC, CNE, CCNA, MCSE
 
this:

Code:
%exclude =  map {$_,$_} @exclude;

is very similar to this and produces the same results in this case:

Code:
for (@exclude) {
   $exclude{$_}=$_;
}

read up on the map function it is very handy.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top