for my $line (@lines) { chomp $line; my @words = split " ", $line; my %local; $local{$_} += $hash{$_} for @words; print join(",", map $local{$_} || 0, @vector), "\n"; }