[Pdx-pm] Not what I expected

paull at peak.org paull at peak.org
Wed Nov 1 10:11:26 PST 2006


I encountered an oddity with sort that surpasses my comprehension. 
Explanations appreciated.

On 5.8.8 (and 5.8.0) I get

Unexpected: Key2 Key1 KeyB KeyD KeyC KeyA KeyB KeyC
MoreLikeIt: Key1 Key2 KeyA KeyB KeyC KeyD

Not only does it not sort, it does not uniquify either.  Bizarre.

Paul Liebert  paull at peak.org


#-------------------snip snip snip------------------
#!/usr/bin/env perl5
use warnings;
use strict;

my %data = ( Key1 => { KeyA => 1,
		       KeyB => 2,
		       KeyC => 3 },
	     Key2 => { KeyB => 2,
		       KeyC => 3,
		       KeyD => 4 },
	     );

my @unexpected = sort uniq( keys( %data ),
			    map{ keys %$_ } values %data );

my @moreLikeIt = sort( uniq( keys( %data ),
			     map{ keys %$_ } values %data ));

print "Unexpected: @unexpected\nMoreLikeIt: @moreLikeIt\n";


sub uniq {
    my %hash;

    @hash{ @_ } = ();

    return keys %hash;
}






More information about the Pdx-pm-list mailing list