LanX has asked for the wisdom of the Perl Monks concerning the following question:
I created a buggy function but my tests miraculously passed none the less.
I narrowed it down to this demo where sort is magically expanding stringified hashrefs like "HASH(0x253f0c8)" ...
(see <--- WTF? in output)
Could someone please explain the difference between sort AoH and sort {$a cmp $b } AoH here
OUTPUT:use v5.12; use warnings; use Data::Dump; my $AoH; debug_loop_join();exit; sub loop_join { my %H; $H{$_} = () for @$AoH; return keys %H; } sub debug_loop_join { $AoH = create_data (.2, 10); ddx [ loop_join ($AoH) ]; ddx [ sort loop_join ($AoH) ]; ddx [ sort {$a cmp $b } loop_join ($AoH) ]; } sub create_data { my ($density, $records) = @_; my @AoH; push @AoH, {map { rand 100 <= $density ? ( $_ => $_ ) : () } "A" . +. "ZZ"} for 1 .. $records; return \@AoH; }
# sort_hash_bug.pl:18: [ # "HASH(0x253f0c8)", # "HASH(0x25350c8)", # "HASH(0x268aa20)", # "HASH(0xeea828)", # "HASH(0x2535548)", # "HASH(0x2535128)", # "HASH(0x268aab0)", # "HASH(0x2535260)", # "HASH(0x268a840)", # "HASH(0x268a918)", # ] # sort_hash_bug.pl:19: [ # <--- +WTF? # [ # {}, # { AZ => "AZ", EM => "EM", RY => "RY" }, # {}, # { GT => "GT", JR => "JR", RZ => "RZ" }, # { HY => "HY" }, # { HT => "HT" }, # { VH => "VH", WU => "WU" }, # { GR => "GR", HH => "HH", HM => "HM", JC => "JC", WJ => "WJ" }, # {}, # { BK => "BK", UH => "UH", XX => "XX", ZN => "ZN" }, # ], # ] # sort_hash_bug.pl:20: [ # "HASH(0x25350c8)", # "HASH(0x2535128)", # "HASH(0x2535260)", # "HASH(0x2535548)", # "HASH(0x253f0c8)", # "HASH(0x268a840)", # "HASH(0x268a918)", # "HASH(0x268aa20)", # "HASH(0x268aab0)", # "HASH(0xeea828)", # ]
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sort AoH buggy?
by choroba (Cardinal) on Dec 09, 2021 at 16:05 UTC | |
by LanX (Saint) on Dec 09, 2021 at 16:15 UTC | |
by LanX (Saint) on Dec 09, 2021 at 16:24 UTC | |
by LanX (Saint) on Dec 09, 2021 at 16:54 UTC | |
by jdporter (Paladin) on Dec 10, 2021 at 13:00 UTC | |
by LanX (Saint) on Dec 10, 2021 at 13:12 UTC | |
by hippo (Archbishop) on Dec 10, 2021 at 15:43 UTC | |
| |
by jdporter (Paladin) on Dec 10, 2021 at 13:33 UTC | |
| |
|