Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

help with a function that will cut out redundancy across two lists

by annakarenina83 (Initiate)
on May 20, 2011 at 05:05 UTC ( [id://905861]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm a humble biologist who has just started to learn Perl (using Sam's 21 days book - I'm only on day seven!) and I need some help with a specific aspect of a script I'm trying to write (just a push in the right direction will do)

Briefly - I need to take two txt files, each with a list of names. There is some redundancy across these two lists (i.e some names appear in both lists) and that is the problem. I need to extract the names from both files (I think I know how to do this - I was just going to put the list of names from each file into two arrays, respectively)and then compare the elements of the arrays and somehow then output a file where it lists each name from both arrays only once (i.e remove the redundancy where a given name appears in both files). The part I need help with is which function/s I could use to only list the names that appear in both lists once (and also include the names that appear in only one of the lists). I hope that was clear.

Any help or a hint would be greatly appreciated. Cheers, Anna

  • Comment on help with a function that will cut out redundancy across two lists

Replies are listed 'Best First'.
Re: help with a function that will cut out redundancy across two lists
by wind (Priest) on May 20, 2011 at 05:11 UTC
Re: help with a function that will cut out redundancy across two lists
by Utilitarian (Vicar) on May 20, 2011 at 05:34 UTC
    Hi Anna,

    Any time you need to remove duplicates in Perl think of using a hash and iterating over the two files incrementing a value indexed by the elements you're cleaning.

    If subsequently you need to find out how many instances of each element there was the associated value will tell you.

    Give this a go and if you have any problems get back to us.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: help with a function that will cut out redundancy across two lists
by ww (Archbishop) on May 20, 2011 at 11:34 UTC

    I can't speak to the quality of the Sams Learn Perl in 21 Days today, but if it's as it was about a decade ago, these suggestions may be helpful:

    1. Check the on-line errata when the material 'taught' before an exercise or problem "just doesn't work."
    2. Recheck against a reliable text (the newest edition of Learning Perl, O'Reilly, for instance).
    3. expand your library.
    4. recycle 21 Days.
Re: help with a function that will cut out redundancy across two lists
by dorko (Prior) on May 20, 2011 at 13:48 UTC
    List::Compare does that and more:
    use List::Compare; use Data::Dumper; @Llist = qw(abel abel baker camera delta edward fargo golfer); @Rlist = qw(baker camera delta delta edward fargo golfer hilton); $lc = List::Compare->new(\@Llist, \@Rlist); #Get those items which appear at least once in both lists (their inter +section). @intersection = $lc->get_intersection; print Dumper \@intersection;

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: help with a function that will cut out redundancy across two lists
by Plankton (Vicar) on May 20, 2011 at 07:00 UTC
    I think you want to do something like this ...
    plankton@ubuntu:~$ cat seen.pl #!/usr/bin/perl -w use strict; my %seen; while(<>) { chomp; $seen{$_}++; } for my $name ( keys %seen ) { print "$name\n"; } plankton@ubuntu:~$ cat names1 delbert james t kirk lisa simpson george of the jungle molly plankton@ubuntu:~$ cat names2 adolf albert molly fortran plankton@ubuntu:~$ ./seen.pl names1 names2 albert molly james t kirk george of the jungle adolf delbert lisa simpson fortran
    ... right?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found