[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] !fwp: weird behavior with sort and unique-ing



I was trying to have some Fun With Perl by writing a unique_list
function.  Here's the simplified version:

sub unique_list {
    return keys %{{ map {($_,undef)} @_ }};
}

This seems to work as expected except when sort() is used.  The
program and output are below, but to make this on-topic, does
anyone have any better ways to do this?

This program:

#!/usr/local/bin/perl -w

use strict;

my @list = qw(a b c d d e a b);

print "list:@list\n";

print "uniqued:"; 
print unique_list(@list), "\n";

print "joined unique list:"; 
print join ',', unique_list(@list);  
print "\n";

print "joined sorted unique list:"; 
print join ',', sort unique_list(@list);  print "\n";

sub unique_list {
    return keys %{{ map {($_,undef)} @_ }};
}


Prints this when run:

list:a b c d d e a b
uniqued:abcde
joined unique list:a,b,c,d,e
joined sorted unique list:a,b,c,d,d,e,a,b


I'm not sure why throwing a sort after the join would do this :(

Any ideas?

- Austin


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe