kzwix has asked for the wisdom of the Perl Monks concerning the following question:
My salutations, ô wise ones.
I've specified a custom block for a sort, so that I look at the value in a related hash to determine the correct order (I'm ordering IPv4 addresses from the most glutton in bandwidth to the least).
So, I wrote:
return sort {return ($refHash->{$b}->{'NBOctets'} - $refHash->{$a}->{'NBOctets'});} keys %$refHash;
($refHash being a reference on a HASH, whose keys are the IPs I want to order, and whose values associated to the keys are references to hashes (namely, for each IP, a hash holding a lot of data, including a 'NBOctets' key, mapping to a number of bytes - The very value I want to order them by, in descending order).
It works quite well, as the following extract of the final result can testify:
172.16.1.104 : 13908480193 octets envoyés, en 100667.7338 secondes, soit un débit moyen de 138162.245915185 octets/seconde. 17.253.55.206 : 771918754 octets envoyés, en 1355.3769 secondes, soit un débit moyen de 569523.321520383 octets/seconde. 172.16.1.120 : 705928230 octets envoyés, en 527922.346599999 secondes, soit un débit moyen de 1337.18194455382 octets/seconde. 17.253.55.204 : 480978512 octets envoyés, en 1630.4618 secondes, soit un débit moyen de 294995.26575845 octets/seconde. 62.210.167.210 : 468692611 octets envoyés, en 594.0011 secondes, soit un débit moyen de 789043.338471932 octets/seconde. 172.16.1.76 : 425684357 octets envoyés, en 261235.63 secondes, soit un débit moyen de 1629.50343718428 octets/seconde. 172.16.1.1 : 288170793399 octets envoyés, en 621335.8493 secondes, soit un débit moyen de 463792.317349231 octets/seconde. 204.92.52.217 : 406751806 octets envoyés, en 373702.6954 secondes, soit un débit moyen de 1088.43690721745 octets/seconde. 13.107.4.50 : 388980279 octets envoyés, en 1578.6308 secondes, soit un débit moyen de 246403.578974894 octets/seconde. 172.16.1.91 : 287922363 octets envoyés, en 70079.6916999999 secondes, soit un débit moyen de 4108.49928154008 octets/seconde. 17.253.55.201 : 281729171 octets envoyés, en 1190.8791 secondes, soit un débit moyen de 236572.437118092 octets/seconde. 188.165.60.91 : 281457541 octets envoyés, en 11396.047 secondes, soit un débit moyen de 24697.8220605794 octets/seconde. 176.31.246.142 : 279936059 octets envoyés, en 11400.6948 secondes, soit un débit moyen de 24554.2981292684 octets/seconde. 62.115.249.136 : 276985087 octets envoyés, en 5152.5337 secondes, soit un débit moyen de 53757.0646068749 octets/seconde. 23.79.119.237 : 272756603 octets envoyés, en 2225.0329 secondes, soit un débit moyen de 122585.424691923 octets/seconde. 23.48.229.216 : 269146838 octets envoyés, en 1108.235 secondes, soit un débit moyen de 242860.799379193 octets/seconde. 92.103.108.76 : 256676616 octets envoyés, en 3320.139 secondes, soit un débit moyen de 77308.9970028363 octets/seconde. 15.216.225.136 : 253665959 octets envoyés, en 319.3335 secondes, soit un débit moyen de 794360.626116583 octets/seconde. 15.217.130.33 : 250905202 octets envoyés, en 262.059 secondes, soit un débit moyen de 957437.836517731 octets/seconde. 17.253.55.203 : 246865654 octets envoyés, en 619.1051 secondes, soit un débit moyen de 398745.954442953 octets/seconde. 172.16.1.63 : 239030327 octets envoyés, en 258208.8964 secondes, soit un débit moyen de 925.724598697445 octets/seconde. 184.25.107.81 : 214707248 octets envoyés, en 1373.6761 secondes, soit un débit moyen de 156301.218314856 octets/seconde. 92.103.108.91 : 207925990 octets envoyés, en 2219.8763 secondes, soit un débit moyen de 93665.5749691999 octets/seconde.
However, as you must surely have noticed, an intruder seems to not have been sorted properly: IP 172.16.1.1, which should have been at the very top of the list, and... well, is not.
Now, I've added debug displays to the sort block, and they seem to give correct results (that is, giving me a positive number if the "gluttonest" IP is $b, and a negative number if it is $a).
So, I wonder... could it be that I've hit some kind of "ceiling" above which the sort function won't work as intended ? I'd much rather use it than code a sort function myself, but, if it's not reliable, I might have to come to that...
Could you maybe help the humble maggot that I am to come one step closer to becoming a nice Perl-Guru-butterfly ?
(I have to mention that I'm working on a huge lot of data, and that I have a nearly-correctly-ordered list in exit, with that IP being the only one badly ordered - I've omitted several hundred IPs (and associated lines) after the ones I displayed)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with a sort result
by 1nickt (Canon) on Jan 14, 2016 at 17:42 UTC | |
by Discipulus (Canon) on Jan 14, 2016 at 17:57 UTC | |
by anonymized user 468275 (Curate) on Jan 14, 2016 at 23:31 UTC | |
by rdfield (Priest) on Jan 15, 2016 at 07:57 UTC | |
by Discipulus (Canon) on Jan 15, 2016 at 09:00 UTC | |
| |
|
Re: Problem with a sort result
by salva (Canon) on Jan 15, 2016 at 08:55 UTC | |
by Discipulus (Canon) on Jan 15, 2016 at 09:23 UTC | |
by kzwix (Sexton) on Jan 15, 2016 at 09:46 UTC | |
by salva (Canon) on Jan 15, 2016 at 09:59 UTC | |
|
Re: Problem with a sort result
by uri (Acolyte) on Jan 15, 2016 at 12:08 UTC | |
by kzwix (Sexton) on Jan 27, 2016 at 14:21 UTC | |
by Anonymous Monk on Jan 27, 2016 at 15:11 UTC |