Solved

perl "vlookup" using a hash?

Posted on 2013-11-11
4
760 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…

752 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