Solved

perl "vlookup" using a hash?

Posted on 2013-11-11
4
772 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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 …
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…
Six Sigma Control Plans

627 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