传递数组值作为输入

Pass array value as input

我写了一个函数,returns IP 地址列表。

我从我的测试脚本 dns.pl 调用该函数并使用 for 循环一次传递一个地址作为命令的输入。

我想将除一个 IP 地址以外的所有 IP 地址作为输入(一次全部)传递给脚本中的命令。

我想检查地址是否匹配 = x.x.x.x。如果是这样,则跳过该地址并将其他地址作为输入传递给脚本中的以下命令。

# 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;
}

使用 grep 从数组中删除不需要的 IP 地址。

$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");