2016-06-13 5 views
1

私はIPアドレスのリストを返す関数を書いています。入力として配列の値を渡す

私のテストスクリプトdns.plから関数を呼び出し、forループを使用して、一度に1つのアドレスをコマンドの入力として渡します。

スクリプトのコマンドに1つの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; 
} 
+2

あなたは、 'perl'のコメントのコメントに対して' // 'は動作しないことに気付きますか? – Sobrique

答えて

3

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"); 
+0

配列から2つの要素を削除したかったのです。私は以下を試した。しかし、それは動作しませんでした。私の@array = grep {$ _!〜((m/@ {$ self - > {'service_ip_of_machine1'}} /) &&($/$ {$ self - > {'service_ip_of_machine2}} /})} @ $ self - > {'machine_ip'}}; – user3587025

関連する問題