[Pdx-pm] sort issues.

Ben Prew bprew at logiccloud.com
Tue Sep 17 11:49:33 CDT 2002


Is it possible to translate the number from x.x to something else, such as
x_x?  This would allow you to key the hash properly although you would have
to do a cmp instead of a <=> in your sort function.  And, I would think that
you could translate from the . notation to the _ notation pretty easily, but
as always YMMV.

--
Ben Prew
http://www.logiccloud.com
bprew (at) logiccloud.com

----- Original Message -----
From: "Roth, Gabrielle" <gabrielle.roth at xo.com>
To: <pdx-pm-list at mail.pm.org>
Sent: Friday, August 30, 2002 10:36 AM
Subject: [Pdx-pm] sort issues.


> Hi all,
> I am having a problem understanding what I need to do to get some hash
keys
> to sort correctly.
>
> background:
> 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---
> #!/usr/local/bin/perl
> #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
> least)
> %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?
>
> -gabrielle
> "I don't mind the rat race but I could do with a little more cheese."
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at mail.pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list




More information about the Pdx-pm-list mailing list