> From: Austin S. Lin [mailto:austin@AL2.com] > Sent: Thursday, January 20, 2000 14:15 > To: fwp@technofile.org > Subject: [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? sort() thinks that unique_list is the sortsub (which is called with no argument, so it always returns the same value) and @list is the list to be sorted. Prefix unary-plus to unique_list and it works as expected. -- Larry Rosler Hewlett-Packard Laboratories http://www.hpl.hp.com/personal/Larry_Rosler/ lr@hpl.hp.com ==== Want to unsubscribe from Fun With Perl? Well, if you insist... ==== Send email to <fwp-request@technofile.org> with message _body_ ==== unsubscribe