Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: One action if an element doesn't exist in array
# 1  
Old 01-11-2011
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:

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

the output of this simple script:
Code:
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
# 2  
Old 01-11-2011
Code:
@array = (1..10);
print "number not found\n" if ! grep (/11/, @array);

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 01-12-2011
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:

Code:
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:
Code:
@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:

Code:
#!/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
# 4  
Old 01-12-2011
The solution bartus gave is quite valid, you'll just have to change the syntax a bit.
Code:
if ( grep /yournumberhere/, @array ) {
    # Code if number was found
} else {
    # Code if number was not found
}


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Group Doesn't Exist

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I'm able to create a group but when I'm trying to delete the group it keeps stating Group Doesn't Exist. I know... (2 Replies)
Discussion started by: GoBoyGo
2 Replies

2. Shell Programming and Scripting

Perform an action if certain text exist in output (PERL)

Hello, I'm attempting to write a tool that checks an IP address for existing PTR records then if there are no PTR records does a ping to see if it response. Then if there is no response, it should print a message saying This is what I have so far. #!/usr/bin/perl $nxdomain =... (4 Replies)
Discussion started by: spartan22
4 Replies

3. Shell Programming and Scripting

Perl: How to check whether my array contains element x

Hi All, I am new to perl I am stuck in simple problem I need your help I want to define a subroutine. sub check_if_entity_exists(@array_to_be_checked,$entityName) I have array as http-listener-1 http-listener-2 http-listener-3 http-listener-4 If i send http-listener-3 my... (1 Reply)
Discussion started by: javaholics
1 Replies

4. Solaris

User directory doesn't exist

Hii all, i create the user useradd -d /home/kk kk passwd kk when i tried to login to kk i get a error user directory doesn't exist then i tried useradd kkk passwd kkkwhen i tried to login to kkk i get the same error user directory doesn't exist. (4 Replies)
Discussion started by: vipinkumarr89
4 Replies

5. Shell Programming and Scripting

previous element in the array perl

Hi, How to get previous/next element in the array perl Example @queue = (1, 2 ,3 , 4); I want to get value of 1 and 2, or, 2 and 3, or 3 and 4...etc and compare to value which one is greater to do that I need to get previous and next element of array ? (1 Reply)
Discussion started by: guidely
1 Replies

6. Programming

Kernel module - How to test if file doesn't exist

Hey, I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part: struct file *fp; /* ... */ fp = filp_open("some/file/on/my/pc", O_RDONLY, 0); if(fp == NULL) { ... (15 Replies)
Discussion started by: disaster
15 Replies

7. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

8. Shell Programming and Scripting

Remove an element from an array in PERL

Hello guys, I have the following question. Consider the following code in PERL for($xeAnumber=0; $xeAnumber<@xeAnumber; $xeAnumber++) { if(@xeAnumber==@final_file) { @final_file=@xeTimeStamp; }... (3 Replies)
Discussion started by: chriss_58
3 Replies

9. Shell Programming and Scripting

Perl delete an element from array

Probably I am not seeing it or I am not using the "delete" correctly I had the following codes but it does not work for me #!/bin/perl -w ... @sysFile1 = (a_b, a_c, a_d); @sysFile2 = (a_c, a_e, b_f); foreach $line1 (@sysFile1){ trim(\$line1); (my $tmp1, my $tmp2) = split/_/,... (6 Replies)
Discussion started by: ahtat99
6 Replies

10. UNIX for Dummies Questions & Answers

What hapens if a group member doesn't exist?

As part of a NIS implementation, (I think) I want to create a group (in /etc/group) that has users that do not exist on the target machine. What effect will this have? Will it cause any problems? Thanks, Gary Cooper (1 Reply)
Discussion started by: Gary Cooper
1 Replies

Featured Tech Videos