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

help with lists

by Anonymous Monk
on Mar 27, 2004 at 20:11 UTC ( [id://340294]=perlquestion: print w/replies, xml ) Need Help??

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

hello, I am a newbie to Perl and need some asssistance cleaning up some data files. I have two files that look something like this...
list #1: bill|552211 joey|600200 mark|996101 drew|460020 paul|124683 suzy|782365 john|741112 mary|685201 fred|415874 list #2 john|568561 mary|002211 fred|875650
What I need to do is to remove any record from list #1 that is also in list #2 based on the person's name. I need to end up with this...
bill|552211 joey|600200 mark|996101 drew|460020 paul|124683 suzy|782365
TIA for any help...

Replies are listed 'Best First'.
•Re: help with lists
by merlyn (Sage) on Mar 27, 2004 at 20:14 UTC
    Smells a bit of homework, but I'll give you this one:
    @ARGV = qw(file2); my %names_in_file2; while (<>) { my ($name) = split /\|/; $names_in_file2{$name}++; } @ARGV = qw(file1); $^I = ".bak"; # create backup file while (<>) { # in-place edit my ($name) = split /\|/; print unless $names_in_file2{$name}; }

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Now that's the way to answer questions you suspect as homework. Using a solution that nobody would generally use under normal circumstances. :)

Re: help with lists
by ambrus (Abbot) on Mar 27, 2004 at 22:39 UTC

    You don't even need perl for this, the unix text utilities are powerful enough.

    If you don't mind that your files get sorted, you can use

    join -t\| -v 1 <(sort first) <(sort second)
    provided that your shell has process substitution.

    (I don't want to cheat by using awk)

    Update 2009 sep 2.

    See Re^2: Joining two files on common field for a list of other nodes where unix textutils is suggested to merge files.

Re: help with lists
by Anonymous Monk on Mar 27, 2004 at 22:03 UTC
    Thank You Randall! Actually this is not a homework assignment. I am about 25 years removed from homework :-) I am helping a friend clean up some data and knew that Perl would most likely offer the quickest and fastest solution. I am learning more and more each day...
Re: help with lists
by muba (Priest) on Mar 27, 2004 at 20:17 UTC
    You might be interested in hashes. That will to the job. Really, this is a true RTFM question, and this is the first time ever I use that word.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-28 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found