Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Using a hash

by l3nz (Friar)
on Dec 02, 2003 at 09:21 UTC ( [id://311563]=note: print w/replies, xml ) Need Help??


in reply to noncase-sensitive sorting

I often use a solution more or less like this, that is based on a hash where the key is the value to be sorted (and in this case is case-insensitive) and the value is the actual string to be printed. It's quite compact and it kills dupes for free. As in this case I don't want it to kill dupes, I append an unique counter to the key so that each key is guaranteed to be unique.

Here's the code:

use strict; my %hDat; my $d; my $n; map( ($hDat{lc $_ . $n++} = $_), <DATA> ); foreach $d (sort keys %hDat) { print $hDat{$d}; } __DATA__ a B c D E Aa Xx A
And here's the result:
a
A
Aa
B
c
D
E
Xx
You know, there is more than one way. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-16 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found