应该在那里找不到数组元素

con*_*ner 0 perl

我正在接收来自某个进程的输出,我希望使用perl从该进程的输出中搜索特定元素,如下所示,但即使有元素,它仍然返回FALSE.我认为我在解析时遇到了错误帮助任何指针.谢谢

流程输出:

origin-server-pool-1
http_TestABC
https_TestABC
Run Code Online (Sandbox Code Playgroud)

脚本:

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

Zai*_*aid 5

很可能你正在使用反引号(qx()).

这就像分配:

@result_listosp = ( "origin-server-pool-1\n",    # Note the
                            "http_TestABC\n",    # trailing
                           "https_TestABC\n" );  # newlines
Run Code Online (Sandbox Code Playgroud)

grep失败的原因是因为"http_TestABC" eq "http_TestABC\n"错误.

解决这个问题的两种方法:

  • chomp @result_listosp; 消除换行结束

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