Solved

perl lookup values from one file in another

Posted on 2013-11-12
2
290 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cpan issue 1 61
Perl 101 11 72
Rename file based on contents in the file? 3 80
Check only file date not subfolder date before getting folder path 3 68
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

14 Experts available now in Live!

Get 1:1 Help Now