use strict; my %hash = ( a => 'A', b => 'B', c => 'C', e => 'E', d => 'D', ); for my $s ( qw( a b c d e ) ) { my $name = $1 if $s =~ /(c)/; $name = $hash{$s} unless defined $name; warn $name || 'undef'; } Output: A at 7.pl line 19. A at 7.pl line 19. c at 7.pl line 19. D at 7.pl line 19. D at 7.pl line 19.