user3587025 user3587025 - 5 months ago 9
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

dns.pl
and use a
for
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

./dns.pl -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(./dns.pl -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

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("./dns.pl -t " . join(',', @ips_minus_value) . " -f .5 -S C");
Comments