From fa67ac02c9f665d835346d14c05d1aa0b8ab2d64 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Wed, 20 Jul 2016 13:13:39 +0200 Subject: [PATCH] Introduce SortAddressHash function. The SortAddressHash function can be used to sort IP-addresses which are stored in a hash into a nice and human read-able format. The sorted result will be returned as an array. Signed-off-by: Stefan Schantl --- modules/Base.pm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/modules/Base.pm b/modules/Base.pm index 0b785f1..41ac981 100644 --- a/modules/Base.pm +++ b/modules/Base.pm @@ -217,4 +217,34 @@ sub GetFileposition ($) { return $position; } +# +## The SortAddressHash function. +# +# This function requires a reference to an hash containing +# IP-addresses, will sort them into a nice looking order +# and return the soreted result as an array. +# +sub SortAddressHash (\%) { + # Dereference the given hash reference and store it + # in a new temporary hash. + my %addresshash = %{ $_[0] }; + + # Loop through the entire hash keys. + foreach my $address (keys(%addresshash)) { + # Convert the address or subnet into binary format. + my @bin_address = &IPOrNet2Int($address); + + # Only store the first result if there are multiple + # one in case of a given subnet. + $addresshash{$address} = $bin_address[0]; + } + + # Sort the addresshash by the binary addresses + # of the stored addresses and save them is an array. + my @sorted_addresses = sort { $addresshash{$a} <=> $addresshash{$b} } keys %addresshash; + + # Return the sorted address array. + return @sorted_addresses; +} + 1; -- 2.39.2