我试图匹配两个变量并获得匹配的字符数。
例如:
$name1 = 'cat';
$name2 = 'dcat';
结果必须为3。
试过这个,但总是打印1
$count = 0;
$count++ while ($name1 =~ /$name2/g);
print "$count\n";
答案 0 :(得分:0)
$count = 0;
$matchstr = "";
foreach (split(//,$name1)){
$matchstr .= $_;
$count++ if $name2 =~ /$matchstr/;
}
print "No of matched characters are: $count \n ";