加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:56:50 所属栏目:百科 来源:网络整理
导读:我想在perl中比较两个正则表达式,但它失败了: Use of uninitialized value in numeric eq == at [… datafile row]. Argument [different regex!] isn’t numeric in numeric eq == […] 我想在数组中存储一个正则表达式,当该值尚不存在时. 因此,我将当前正
我想在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])

此外,你在for循环中超越了regex_array的结尾:

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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读