Solved

perl "vlookup" using a hash?

Posted on 2013-11-11
4
745 Views
Last Modified: 2013-11-11
Hi,

I have a text file with two columns like follows

9781      Person1
978       Person2
9782      Person3

I then have an ID that I want to look up from this file - returning the 2nd column for the first match it finds.

For example, if my ID = “978”, I want it to return “Person2” as I want it to be an exact match against columnA.

ID “9782” would then return “Person3”

Can this be done with a hash and using exist?

(This probably sounds so basic that it reads like a homework question, it’s really not!)
0
Comment
Question by:yelbow
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39640133
Yes.  As a one-liner, this would do it.  It can be easily expanded into a script (or portion thereof).
perl -ne '($id,$per) = split; $data{$id} = $per; END { print $data{978}, "\n" if exists($data{978}) }'

Open in new window

0
 

Author Comment

by:yelbow
ID: 39640227
Thanks!  I've realised I need to complicate this further - it seems that ColumnA doesn't always contain a unique value.  For example, the data could be:

9781      Person1
978       Person2
978        Person4
9782      Person3

I think a has requires a unique match on the key (?) and if not returns the last one it finds (?) (at least it seems that way!)

is there a way to return the first match it finds on $id?

(Apologies for extending the question!)
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 39640248
First match
perl -ne '($id,$per) = split; $data{$id} = $per unless exists($data{$id}); END { print $data{978}, "\n" if exists($data{978}) }'

Open in new window


All matches
perl -ne '($id,$per) = split; $data{$id} = [] unless $data{$id}; push @{$data{$id}}, $per; END { print "@{$data{978}}\n" if exists($data{978}) }'

Open in new window

0
 

Author Closing Comment

by:yelbow
ID: 39640264
Thanks!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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