Perl 找不到应存在的数组元素

Perl 找不到应存在的数组元素,perl,Perl,我从某个进程接收到这样的输出,我想使用perl从该进程的输出中搜索特定的元素,我已经做了如下操作,但即使有元素,它仍然返回FALSE。我认为我在解析过程中出错,这有助于任何指针。 谢谢 过程的输出: origin-server-pool-1 http_TestABC https_TestABC 脚本: use strict; use warnings; my @result_listosp; #assigned from output process given above my $o

我从某个进程接收到这样的输出,我想使用perl从该进程的输出中搜索特定的元素,我已经做了如下操作,但即使有元素,它仍然返回FALSE。我认为我在解析过程中出错,这有助于任何指针。 谢谢

过程的输出:

origin-server-pool-1
http_TestABC
https_TestABC
脚本:

use strict;
use warnings;


my @result_listosp; #assigned from output process given above


my $osp="http_TestABC";
my $status_osp_check= check_if_entity_exists($osp,@result_listosp);
print $status_osp_check;


sub check_if_entity_exists() 
{
    my $entity = shift;
    my @entityarray = @_;


    my $status="FALSE";

    if ( grep { $_ eq $entity} @entityarray) {
        $status="TRUE";
        return $status;
    } 
    else {
        return $status;
    }
}

很可能您正在使用反勾号(
qx()

这就像分配:

@result_listosp = ( "origin-server-pool-1\n",    # Note the
                            "http_TestABC\n",    # trailing
                           "https_TestABC\n" );  # newlines
grep
失败的原因是因为
“http\u TestABC”eq“http\u TestABC\n”
为false

解决此问题的两种方法:

  • chomp@result\u listosp以消除换行符结尾

  • 使用正则表达式匹配(
    =~
    )而不是精确匹配(
    eq


您很可能正在使用反勾号(
qx()

这就像分配:

@result_listosp = ( "origin-server-pool-1\n",    # Note the
                            "http_TestABC\n",    # trailing
                           "https_TestABC\n" );  # newlines
grep
失败的原因是因为
“http\u TestABC”eq“http\u TestABC\n”
为false

解决此问题的两种方法:

  • chomp@result\u listosp以消除换行符结尾

  • 使用正则表达式匹配(
    =~
    )而不是精确匹配(
    eq