user3587025 user3587025 -5 years ago 159
Perl Question

Pass array value as input

I have written a function that returns a list of IP addresses.

I call the function from my test script
and use a
loop to pass one address at a time as input to a command.

I want to pass all but one IP address as input (all at once) to a command in the script.

I want to check if the address matches = x.x.x.x. If so then skip that address and pass the other addresses as input to the below command in the script.

# The IP address that are retuned by the function should be passed here as input all at once. Except one ip address

./ -t ipaddress1,ip address2,.... ipadress,n -f .5 -S C

# function call to get the list of ip addresses

$self->{'machine_ip'} = $self->{'queryObj'}->get_machine_ip( $self->{'vip_owner'} );

# Currently, I'm passing one ip address at a time using foreach
# But, I want to pass all but one ip address all at once to the below command as input.

foreach my $ip ( @{ $self->{'machine_ip'} } ) {
$self->{'exec_obj'}->execute(./ -t ipaddress1,ip address2,.... ipadress,n -f .5 -S C);

sub get_machine_ip {

my ( $self, $vip_owner ) = @_;
my @ip = ();
my $sql_query = $self->{queryObj}->execute("select machineIP from sripd_peer_Status where frontend=$vip_owner");
my $records = $self->{queryObj}->result();

foreach my $row ( @$records ) {
push @ip, $row->{machineIP};

return \@ip;

Answer Source

Use grep to remove the unwanted IP address from the array.

$self->{machine_ip} = $self->{queryObj}->get_machine_ip($self->{vip_owner});

my @ips_minus_value = grep {$_ ne 'IP_ADDRESS_TO_REMOVE'} @{$self->{machine_ip}};
$self->{exec_obj}->execute("./ -t " . join(',', @ips_minus_value) . " -f .5 -S C");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download