1

If $client_id doesn't exist in @ping_host it has to pass the if condition, but it only pass when the @ping_host is empty. Am I doing anything wrong?

if ( !grep( $client_id, @ping_host ) ) {
            print "Client Id $client_name doesn't exist \n";
 }
user1595858
  • 3,700
  • 15
  • 66
  • 109

2 Answers2

3

It's somewhat inefficient to use grep for this, because it returns all matches in the array, whereas you only care if it has at least one. You could use none for this like:

use List::MoreUtils qw(none);
if(none { $client_id eq $_ } @ping_host) {
    print "Client Id $client_name doesn't exist \n";
}
AKHolland
  • 4,435
  • 23
  • 35
1

First grep parameter should be a test which returns boolean value for each element of @ping_host array,

if ( !grep( $client_id eq $_, @ping_host ) ) {
  print "Client Id $client_name doesn't exist \n";
}
mpapec
  • 50,217
  • 8
  • 67
  • 127