#!/usr/bin/perl use warnings; use strict; my $str = 'abcdefghijkl'; my %match = (ab => 0, abcd => 0, xyz => 0,); for my $key (keys %match){ if ($str =~ /$key/){ $match{$key}++; } else { print "$key not found in $str\n"; } } for my $k (keys %match){ print "key: $k value: $match{$k}\n"; }