foreach $key (keys %hash) { my $cmd = "$program $key $matrix "; $cmd .= "$path/CISCO_$_.clean " for @{$hash{$key}}; system($cmd) == 0 # system returns 0 on success or die "system '$cmd' failed: $?"; }