Solved

perl "vlookup" using a hash?

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Tutorial 6 127
perl match and sort unique result 2 122
perl script to search for files containing text 3 65
combine multiple lines 2 23
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

19 Experts available now in Live!

Get 1:1 Help Now