比较两个正则表达式与If-Statement失败

Tyz*_*zak 4 regex perl compare

我想在perl中比较两个正则表达式,但它失败了:

在[... datafile&row]的数字eq <==>中使用未初始化的值.参数[不同的正则表达式!]在数字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++;

}
Run Code Online (Sandbox Code Playgroud)

Mat*_*Mat 6

==用于比较数字.使用eq比较字符串:

if ($tmpvar eq $regex_array[$i]) 
Run Code Online (Sandbox Code Playgroud)

此外,你将超越regex_array你的for循环结束:

for ($i=0; $i < $tmparraylength ;$i++)
              ^ this must not be <=
Run Code Online (Sandbox Code Playgroud)

最后,你做的工作太多了.使用哈希,它会自动"删除重复的密钥".

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;
Run Code Online (Sandbox Code Playgroud)