我想在 perl 中比较两个正则表达式,但失败了:
Use of uninitialized value in numeric eq <==> at [... datafile & row]. Argument [different regex!] isn't numeric in numeric eq <==> [...]
当值不存在时,我想将正则表达式存储在数组中。
因此,我将当前的正则表达式 (tmpvar) 与数组中的所有元素进行比较。
一般有可能还是我必须使用变通方法?
$pass = new String::Random;
my $array_counter=0;
my @regex_array = ();
push(@regex_array, $pass->randregex($regex_from_file));
#print "@regex_array";
$array_counter++;
while ($array_counter != $anzahl_regex)
{
print $array_counter;
my $tmpvar = $pass->randregex($regex_from_file);
my $tmpcheck = 0;
my $tmparraylength = scalar (@regex_array);
for ($i=0; $i<= $tmparraylength ;$i++)
{
if ($tmpvar == $regex_array[$i]) # ERROR
{
$tmpcheck =1;
}
}
if ($tmpcheck == 0) # es wurde kein duplikat gefunden
{
push(@regex_array,$tmpvar);
$arraycounter++;
}
$arraycounter++;
}
最佳答案
==
用于比较数字。使用 eq
比较字符串:
if ($tmpvar eq $regex_array[$i])
此外,您将超出
regex_array
的末尾在你的 for 循环中:for ($i=0; $i < $tmparraylength ;$i++)
^ this must not be <=
最后,你做的工作太多了。使用散列,它会自动“删除重复的键”。
my %temp_hash;
while (scalar(keys %temp_hash) < number_of_things_you_want) {
$temp_hash{$pass->randregex($regex_from_file)}++;
}
@regex_array = keys %temp_hash;
关于regex - 比较两个带有 If-Statement 的正则表达式失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7754717/