Perl: One action if an element doesn't exist in array

Hello,

I want to run one (not multiple) action if an element doesn't exist in array.
for example:

@array = (1..10);
foreach $el (@array)
{
 if ($el != 11)
 {
  print "number not found\n";
 } 
}

the output of this simple script:

number not found
number not found
number not found
number not found
number not found
number not found
number not found
number not found
number not found
number not found

But what I want to do is printing only one "number not found" or on other words, run one action if number doesn't exist in the array.

Thanks in advance.
Ahmed

@array = (1..10);
print "number not found\n" if ! grep (/11/, @array);
1 Like

Many thanks for your support.

It works fine with my simple example but I unfortunatly I still don't know how to make it for my actual script.

I want to run some cisco commands after checking that exact number doesn't exist in the following command output:

FWSMLAN1# sh vlan
203, 300-324 , 326, 329-331 , 333-340 , 342, 344, 346-351 , 353, 355-356 , 370, 388-390 , 399-401 , 404, 515-517 , 629, 658, 830, 834, 838, 843-844 

the commands want to be written if exact number doesn't exist:

@output = $con->cmd("show running-config interface vlan $vlan_id$");
print join("\n", @output);
my @output_line = split (/\s+/, $output[2]);
if ("$output_line[0]" eq "ERROR:")
{
 @output = $con->cmd("configure terminal");
 print join("\n", @output);
 @output = $con->cmd("interface vlan $vlan_id$");
 print join("\n", @output);
 @output = $con->cmd("no shut");
 print join("\n", @output);
 @output = $con->cmd("nameif $nameif$");
 print join("\n", @output);
 @output = $con->cmd("security-level $sec_level$");
 print join("\n", @output);
 @output = $con->cmd("ip address $IP$ $mask$ standby $standby_IP$");
 print join("\n", @output);
 @output = $con->cmd("exit");
 print join("\n", @output);
}

and here is my script:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Opsware::NAS::Connect;
 
my($host, $port, $user, $pass) = ('localhost','$tc_proxy_telnet_port$','$tc_user_username$','$tc_user_password$');
my @output;
my $con = Opsware::NAS::Connect->new(-user => $user, -pass => $pass, -host => $host, -port => $port);
$con->login();
 
$con->connect( '192.168.15.15' ) or die "Failed to connect.";
$con->cmd("terminal length 0");
@output = $con->cmd("show vlan");
print join("\n", @output);
my @vlan = split (/,/, $output[1]);
for (my $i=0; $i <= $#vlan; $i++)
{
 if ( "$vlan[$i]" =~ /(\d+)-(\d+)/ )
 {
  print "start: $1, end: $2\n";
  for (my $j=$1; $j <= $2; $j++)
  {
   if ($j eq $vlan_id$)
   {
    print "vlan $j found\n";    
   }
  }
 }
 else
 {
  if ($vlan[$i] eq $vlan_id$)
  {
   print "vlan $vlan[$i] found\n";
  }
 }
} 
 
@output = $con->disconnect();
 

Appreciate your advise.
Thanks in advance,
Ahmed

---------- Post updated 01-12-11 at 03:19 AM ---------- Previous update was 01-11-11 at 11:42 AM ----------

Hello,

I think I make you confused.

in simple words,
I want to run multiple commands when "string" is member of array and then
run other multiple commands when "string" is NOT member of array.

Thanks in advance,

Appreciate your reply,
Ahmed

The solution bartus gave is quite valid, you'll just have to change the syntax a bit.

if ( grep /yournumberhere/, @array ) {
    # Code if number was found
} else {
    # Code if number was not found
}