Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: alphabetize a hash ignoring upper/lower cases

by vladb (Vicar)
on May 11, 2003 at 06:12 UTC ( #257198=note: print w/ replies, xml ) Need Help??


in reply to alphabetize a hash ignoring upper/lower cases

Just compare the lowercase/uppercase variants of the strings. Make sure you convert all of them to common case (using the lc or uc methods). For example, I'd try a code like this:

foreach $orf (sort {lc($mips{$a}) cmp lc($mips{$b})} keys %mips) { push @mips, $mips{$orf}; }
(note: didn't test the code snippet, but confident it should work)

_____________________
# Under Construction


Comment on Re: alphabetize a hash ignoring upper/lower cases
Select or Download Code
Re: Re: alphabetize a hash ignoring upper/lower cases
by halley (Prior) on May 11, 2003 at 12:07 UTC

    For very small hashes, performing two lc(...$a...) calls per sort iteration is okay.

    For larger hashes, performing all those calls per sort iteration will bog you down. As the number of items increases, the number of sorting comparison increases geometrically. If you need speed, look around for the "Schwartzian Transform" (named after our own merlyn).

    my @ordered = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, expensive_function($_) ] } # such as lc() @unordered; # such as keys %hash

    This calls an expensive setup operation once per item and saves the resulting value, then sorts according to these temporary values, then strips away the temporary values, leaving the original items, but in the right order.

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://257198]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-09-17 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (91 votes), past polls