I'm writing a sort block to handle data of different formats.

My data (switch portnames) takes one of four formats:

my %oids = ( "ifName" => { 1 => "Gi1/0/1", 2 => "Gi1/0/0", 3 => "Gi1/1/1", 4 => "Gi2/0/0", 5 => "Gi2/0/1", }, );
- or: my %oids = ( "ifName" => { 1 => "1", 2 => "2", 3 => "3", 4 => "4", 5 => "5", }, ); - or: my %oids = ( "ifName" => { 1 => "2/1", 2 => "4/5", 3 => "15/1", 4 => "1/1", 5 => "1/2", }, ); - or: my %oids = ( "ifName" => { 1 => "Fa0/1", 2 => "Fa0/2", 3 => "Fa3/1", 4 => "Gi0/1", 5 => "Gi0/0", }, );

The goal is to get an array of the hash keys in value-sorted order. (I'm using a HoH because I have lots of other data referenced by keys of %oids.)

Here's what I've come up with:

my (@a, @b); my @port_ifIndices = sort { if ($oids{ifName}{$a} =~ /\//) { @a = split(/\//, $oids{ifName}{$a}); @b = split(/\//, $oids{ifName}{$b}); } else { @a = $oids{ifName}{$a}; @b = $oids{ifName}{$b}; } if ($a[0] =~ /[A-Za-z]/) { $a[0] ne $b[0] ? $a[0] cmp $b[0] : ($a[1] <=> $b[1] ? $a[1] <= +> $b[1] : $a[2] <=> $b[2]); } else { $a[0] != $b[0] ? $a[0] <=> $b[0] : ($a[1] <=> $b[1] ? $a[1] <= +> $b[1] : $a[2] <=> $b[2]); } } keys(%{ $oids{ifName} }); foreach my $ifIndex (@port_ifIndices) { print ("$ifIndex: $oids{ifName}{$ifIndex}\n"); }

This gives me (using my first data example):

2: Gi1/0/0 1: Gi1/0/1 3: Gi1/1/1 4: Gi2/0/0 5: Gi2/0/1
...which is exactly what I want.

I'm aware that the recomputation of my sort keys with each iteration is not recommended for speed. I'm following the "build it up by pieces" method for a Schwartzian Transform (my first, woo-hoo) from _Effective Perl Programming_, and I need to start with a working sort. :)

The questions:

1. Is there a more efficient way to handle my initial computation of the @a and @b arrays?

2. Am I violating any rules/best practices by having the second if/else block (the one that actually chooses the sort operation) within my sort block? If so, what are my other options?

3. The code above gives me my desired result with my existing data set. In case that changes in the future, I'm trying to figure out how to split "Fa10/0/1" into [Fa, 10, 0, 1] but all my ideas involve substrings & splits & freaky array manipulations and quickly get very ugly. Ideas?

Thank you!

In reply to best practices for a complex sort + splitting an alphanumeric string by gabrielle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.