my %results; for my $tok ( split /\s+/, $input ) { $results{ $1 } = $2 if $tok =~ /^ (.) = (\d+) $/x; } for my $k ( qw( a b c ) ) { print "$k is ", exists $results{ $k } ? $results{ $k } : 'missing', "\n"; }