1

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;
}
1
  • 2
    You do realise that // doesn't work for comments in perl right? Commented Jun 13, 2016 at 14:58

1 Answer 1

3

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");
Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to remove two elements from the array. I tried the below. But it didn't work. my @array = grep {$_ !~ ((m/@{$self->{'service_ip_of_machine1'}}/) && (m/@{$self->{'service_ip_of_machine2}}/)) } @{$self->{'machine_ip'}};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.