Solved

perl lookup values from one file in another

Posted on 2013-11-12
2
287 Views
Last Modified: 2013-11-12
Hi,

I asked a question yesterday (http://rdsrc.us/JU6UCg) which was answered but I think I need to go back and start at the beginning.

I have a text file with two columns like follows (the real file has c.100k lines)

9781      Person1
978        Person2
9782      Person3
978        Person4

I then have a second text file that has one column of data as follows:

978
9781


I want to loop round this second file and using the ID lookup the corresponding line in file1 and return the value in column.  I need to loop it as one the value is retrieved I need to do further processing on it, but that’s outside this question.

Note that in file1 the data in the first column isn’t unique  - where more than 1 match is found, I want it to return a null (i.e undtermined)

So, given the IDs in file 2, I would want to retrieve:

undetermined
Person1


How do I properly read file1 into a hash and then lookup values in it whilst looping through a second file?
0
Comment
Question by:yelbow
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39641554
#!/usr/bin/perl
use strict;
use warnings;
open F,"<a text file" or die $!;
my %f;
while( <F> ){
    next unless my @F=split;
    push @{$f{$F[0]}}, $F[1];
}
open F,"<second text file" or die $!;
while( <F> ){
   my($id)=/(\S+)/;
   print @{$f{$id}||[]}==1?$f{$id}[0]:"undetermined","\n";
}
0
 

Author Closing Comment

by:yelbow
ID: 39641719
Thanks, works a treat - will now go and decipher it.  Thanks again.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now