use strict; use warnings; my( %file, $idx, %cat,$x); $idx=1; # Start at 1 to avoid using the //= operator while (<>){ # read all files in @ARGV chomp; my ($c,$val)=split; # Separate the category from the value next unless $c; # Skip blank lines $file{$ARGV} ||= $idx++; # Increment $idx for each new file $cat{$c}[ $file{$ARGV} ] = $val; # Save in $cat{Category}[FileIdx] } print "\t",map ({"$_\t"} sort keys %file),"\n"; # List of files my @FileIndexes = map {$file{$_}} sort keys %file; for my $k (sort keys %cat){ print "$k\t",map({(defined($x=$cat{$k}[$_])?$x:"")."\t"} @FileIndexes),"\n"; }