[Pdx-pm] sort issues.

Greg Cheong gcheong at acm.org
Tue Sep 17 12:33:38 CDT 2002


Gabrielle, do you want the keys returned in the order of insertion? In that
case try the Tie:IxHash module (see pg.139 sec 5.6 of Perl Cookbook). As for
the missing 1.10, its probably being converted to 1.1 and overwriting the
original 1.1 key, value pair when you create the hash. Did you try quoting
the keys when you create the hash?

Cheers,
Greg


----- Original Message -----
From: "Ben Prew" <bprew at logiccloud.com>
To: <pdx-pm-list at mail.pm.org>
Sent: Tuesday, September 17, 2002 9:49 AM
Subject: Re: [Pdx-pm] sort issues.


> 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
>
> _______________________________________________
> 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