my %prices; # read all files: for $fn (@files){ open (my $file, '<', $fn) or die "Can't open '$fn' for reading: $!"; while (<$file>){ chomp; my @items = split m/;/, $_; push @{$prices{$items[0]}, $items[1]; } } # print output: for my $k (sort keys %prices){ print "$k;", join(';', @{$prices{$k}}), $/; }