in reply to Matching hash keys from different hashes and utilizing in new hash
if I understand well, you're looking for records having the same identifier (same first column) of file 1 and file 2 and want to output the data of common records.
This can be much simpler.
Start by reading the first file, store the data into a hash. Then read the second file line by line; if you find the identifier of a record of file 2 in the hash containing the data of file 1, then output it with the desired format. Something like that (untested because there is not enough sample data):
BTW, this should probably work with many more columns in your file.#!/usr/bin/perl use strict; use warnings; my ($file1, $file2) = @ARGV; my %hash_file1; open my $FILE1, "<", $file1 or die "Cannot open $file1 for processing! +\n"; while (my $line = <$FILE1>) { my ($key, @fields) = split /\s+/, $line; $hash_file1{$key} = join ":", @fields; } close $FILE1; open my $FILE2, "<", $file2 or die "Cannot open $file2 for processing! +\n"; while (my $line = <$FILE2>) { my ($key, @fields) = split /\s+/, $line; my $rest_of_line = join ":", @fields; if (exists $hash_file1{$key}) { # this is a common record +(same identifier) print $key, ":", $hash_file1{$key}, ":", $rest_of_line, "\n"; } } close $FILE2;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Matching hash keys from different hashes and utilizing in new hash
by huck (Prior) on Oct 21, 2017 at 21:54 UTC | |
by Laurent_R (Canon) on Oct 21, 2017 at 23:01 UTC | |
|
Re^2: Matching hash keys from different hashes and utilizing in new hash
by FIJI42 (Acolyte) on Oct 22, 2017 at 05:28 UTC | |
by Laurent_R (Canon) on Oct 22, 2017 at 07:31 UTC |