0

I am receiving output from some process as this and i want to search particular element from the out put of that process using perl I have done as below but still it return FALSE even if there is element.I think something i am doing wrong in parsing help any pointers . Thanks

Output from process:

origin-server-pool-1
http_TestABC
https_TestABC

Script:

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;
    }
}

1 Answer 1

5

Most likely you are using backticks (qx()).

This is like assigning:

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

The reason why the grep is failing is because "http_TestABC" eq "http_TestABC\n" is false.

Two ways to fix this:

  • chomp @result_listosp; to eliminate the newline endings

  • Use a regex match (=~) instead of an exact match (eq)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.