[Pdx-pm] sort issues.

Roth, Gabrielle gabrielle.roth at xo.com
Fri Aug 30 12:36:46 CDT 2002

Hi all,
I am having a problem understanding what I need to do to get some hash keys
to sort correctly.

I have a script that uses snmp to collect information (port names, duplex
settings, etc) about network switches;  ultimately this information is
transferred to monitoring and mapping software.  Currently I am gathering
this information into several hashes;  the keys for the hashes are the
module.port assignment on the switch (eg 1.1, 1.2, etc).

---begin code snippet---
#Note:  this hash is created when the script is run.
#I hard-coded it in here to simplify the problem (that was my theory, at
%portName = (
        1.1 => "smith",
        1.3 => "gunks",
        1.4 => "horsethief",
        1.10 => "tahquitz",
        1.12 => "carderock",
        2.1 => "seneca",
        2.2 => "newriva",
        2.14 => "lincoln",

#from eg http://www.perlfect.com/articles/sorting.shtml
@key_array = sort {$a <=> $b} keys(%portName);

print ("port\tname\n");

foreach $port (@key_array) {
        print ("$port\t$portName{$port}\n");
---end code snippet---

Ideally, my final output would look something like this:
port	name
1.1	smith
1.3	gunks
1.4	horsethief
1.10	tahquitz
1.12	carderock
2.1	seneca
2.2	newriva
2.14	lincoln

Instead, I get this:
port	name
1.1	tahquitz
1.12	carderock
1.3	gunks
1.4	horsethief
2.1	seneca
2.14	lincoln
2.2	newriva

I have two problems:
1.  Obviously, the order isn't right.  I thought the <=> operator would do a
numerical sort, but apparently that's just for integers?  I'm not any better
off than if I just did a regular sort.
2.  One of (1.1|1.10) goes missing;  I thought maybe because the <=>
evaluated them as equivalent, but it does this with the "cmp" operator as
well.  I tried quoting the keys, but that didn't help either.

What am I missing?

"I don't mind the rat race but I could do with a little more cheese."

More information about the Pdx-pm-list mailing list