Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Deleting elements of one list using another

by nemesdani (Friar)
on Aug 02, 2012 at 08:40 UTC ( #984984=note: print w/ replies, xml ) Need Help??


in reply to Deleting elements of one list using another

Untested and unnecessary verbose, but I think you'll get the idea:

use strict; use warnings; my @arr1 = ("John, ABC, 42","Jane, XYZ, 34","Jessica, GHI, 21"); my @arr2 = ("ABC", "XYZ"); my @arr3; my %seen; foreach my $arr1(@arr1) { my @splitted = split (/,/, $arr1); foreach my $arr2(@arr2) { if (grep(/$arr2/, @splitted)) { unless ($seen{$arr1} ){ push (@arr3, $arr1); } } } } for (@arr3) {print "$_\n";}

I'm too lazy to be proud of being impatient.


Comment on Re: Deleting elements of one list using another
Download Code
Re^2: Deleting elements of one list using another
by Sj03rd (Initiate) on Aug 02, 2012 at 12:16 UTC
    Great, this works fine for me. Flawless code except for  @splitted in the  grep command which should be  @splitted[0]. Thanks!
      You mean $splitted[0]? :)
      But no. splitted[0] contains "John" in the first loop, than "Jane", finally "Jessica". You'd like to grep the whole "John ABC 42" for ABC, don't you?
      Otherwise I haven't understand the problem well enough.

      I'm too lazy to be proud of being impatient.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2015-07-06 09:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls