use strict; use warnings; use feature qw{ say }; sub load { my ($file, $table, $phase) = @_; open my $in, '<', $file or die "$file: $!"; while (<$in>) { chomp; my @columns = split /\t/; my $id = join '_', @columns[0, 1]; die "Duplicate $id." if 'first' eq $phase && exists $table->{$id}; push @{ $table->{$id} }, $columns[2]; say join "\t", @columns[0, 1], @{ $table->{$id} } if 'print' eq $phase; } } my %table; my $phase = 'first'; while (my $file = shift @ARGV) { load($file, \%table, $phase); $phase = 1 == @ARGV ? 'print' : ''; }