From 8f75194cd89d364f8b80c49fd3be520c8a54f4d9 Mon Sep 17 00:00:00 2001 From: Fabian Ebner Date: Mon, 10 May 2021 14:18:16 +0200 Subject: [PATCH] network: add unique_ips function Signed-off-by: Fabian Ebner --- src/PVE/Network.pm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm index 48268b1..bb574e0 100644 --- a/src/PVE/Network.pm +++ b/src/PVE/Network.pm @@ -665,4 +665,24 @@ sub canonical_ip { return $ip_obj->canon(); } +# List of unique, canonical IPs in the provided list. +# Keeps the original order, filtering later duplicates. +sub unique_ips { + my ($ips) = @_; + + my $res = []; + my $seen = {}; + + for my $ip (@{$ips}) { + $ip = canonical_ip($ip); + + next if $seen->{$ip}; + + $seen->{$ip} = 1; + push @{$res}, $ip; + } + + return $res; +} + 1;