foreach (@input) { if( exists $list{ $_ } ) { push @name_nums, $list{$_}; } else { push @name_names, $_; } } #### foreach my $input (@input ) { push @name_nums, ( exists $list{ $input } ? $list{ $input } : $input ); } ## following will not work if $list{ $input } can be 0 or undef, ## but here's another way foreach my $input ( @input ) { push @name_nums, ( $list{ $input } || $input ); }