#!/usr/bin/perl -w

# This script processes input files to create a hash table, then looks up values from STDIN
# Usage: map [-s separator] [-k] input_files... < lookup_keys

use strict;
use warnings;

use Getopt::Std;

our ($opt_s, $opt_k);

our %hash;
BEGIN {
	# Parse command line options
	getopts('s:k');
	$opt_s //= '\t';  # Default separator is tab
	$opt_k //= 0;     # Default is not to print keys

	# Process input files and build hash table
	for my $file (@ARGV) {
		open my $fh, "<", $file
			or die "can't open: $file";
		while (<$fh>) {
			chomp;
			my ($k, $v) = split /$opt_s/, $_, 2;
			$hash{$k} = $v;
		}
	}
}

# Process STDIN, lookup values in hash table, and print results
while (<STDIN>) {
	chomp;
	my $v = $hash{$_};
	print $_ if $opt_k;  # Print key if -k option is set
	print "$v" if $v;    # Print value if it exists
	print "\n";
}
