#!/usr/bin/perl use strict; use warnings; my %file1_data; my %file2_data; print "\n\n"; # Process data files ProcessFile( "File1", \%file1_data ); ProcessFile( "File2", \%file2_data ); # Format output my @keys = sort keys %file1_data; foreach my $key ( @keys ) { my $target = exists $file2_data{$key} ? $file2_data{$key} : ''; print "$key $file1_data{$key} $target\n"; } # Process a file write data into supplied hash ref. sub ProcessFile { my $filename = shift; my $data = shift; # Pass data by reference - big hashes used here. open( DATAFILE, '<', $filename ) or die "Unable to open $filename - $!\n"; # Store data in hash. # Only the last instance of any key is stored. while ( my $line = ) { my ($key, $value) = split /\s+/, $line, 2; # split into max of 2 fields. $data->{$key} = $value; } close( DATAFILE ) or die "Unable to close $filename - $!\n"; }