in reply to best practices for a complex sort + splitting an alphanumeric string

I'd use something similar to one of the techniques found by searching for natural sort. Likely:

my @sortedIdx= map { unpack "N", substr($_,-4) } sort map { my $key = $oids{ifName}{$_}; $key =~ s[(\d+)][ '0' . pack "N", $1 ]ge; $key . pack "CN", 0, $_ } keys %{$oids{ifName}};

Though I'm a little curious why your integer indices are used to index a hash not an array. If your keys aren't just integers, then you'll need:

my @keys= ( keys %{$oids{ifName}} )[ map { unpack "N", substr($_,-4) } sort map { my $key = $oids{ifName}{$_}; $key =~ s[(\d+)][ '0' . pack "N", $1 ]ge; $key . pack "CN", 0, $_-1 } 1 .. keys %{$oids{ifName}} ];

(Updated)

- tye        

Replies are listed 'Best First'.
Re^2: best practices for a complex sort + splitting an alphanumeric string (natural)
by gabrielle (Novice) on Jan 13, 2006 at 20:42 UTC
    ...searching for natural sort

    Thanks! I had not heard that term before. That will be very useful.

    Though I'm a little curious why your integer indices are used to index a hash not an array.

    That's a really good question & embarassingly, I have no good technical answer. I suspect it is both because this is recycled code that I'm reworking to handle a larger data set, and the original keys were not integers; and because I work with hashes so much more than arrays that it's just a more comfortable method for me.

    Thanks for pointing that out - I will re-think how I'm handling this data structure.

    Update:After dinking around with this a bit: The integer index is an important key into other data structures (it's an ifIndex, if you are familiar with SNMP MIB-II). This was not obvious from my small example, sorry. The indices are not always sequential - often a few (or several, gah!) are missing here and there. For me, it's clearer (and therefore easier to maintain) to think of the integer as a hash key. Trying to work with an array for that was a very interesting exercise, thanks. :)