Solved

perl lookup values from one file in another

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using Perl to parse rows 7 97
Get a Perl script to return content from a module 7 82
Perl Write to Specific line in a file 15 106
Regex rule to match two different url 5 74
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
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…

856 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