You can read the second file into a hash if the values in the first column are unique. By 'unique' I mean that '153814_at' occurs no more than once in the second file, so you can use it as a key in your hash whose value is 0.09276438. After building your hash by reading through your second file you can open your first file. For each record in your first file you can split the record into its two columns and see if an entry corresponding to the value in the first column occurs in the hash. If it does you can take the value associated with that key from the hash and you have the three data items that you want to print.
For example:
file1.txt
beans green
lime green
banana yellow
carrot orange
file2.txt
apple fruit
carrot vegetable
beans vegetable
banana fruit
spinach vegetable
script.pl
#!/usr/bin/perl
use strict;
use warnings;
my $filename1 = 'file1.txt';
my $filename2 = 'file2.txt';
my %categories;#Hash to store key=>value pairs from file2.txt
open my $fh, '<', $filename2 or die "Failed to open $filename2: $!";
while (<$fh>){
chomp;
my ($name, $cat) = split;
$categories{$name} = $cat;
}
close $fh;
open $fh, '<', $filename1 or die "Failed to open $filename1: $!";
while (<$fh>){
chomp;
my ($name, $colour) = split;
my $cat;
if (exists $categories{$name}){
$cat = $categories{$name}
}
else{
$cat = "***$name Not Found in $filename2***";
}
print "$name\t$colour\t$cat\n";
}
close $fh;
Output:
beans green vegetable
lime green ***lime Not Found in file2.txt***
banana yellow fruit
carrot orange vegetable
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1
Question Answered as of 1 Year Ago by
d5e5