gabrielle has asked for the wisdom of the Perl Monks concerning the following question:
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):
...which is exactly what I want.2: Gi1/0/0 1: Gi1/0/1 3: Gi1/1/1 4: Gi2/0/0 5: Gi2/0/1
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: best practices for a complex sort + splitting an alphanumeric string
by revdiablo (Prior) on Jan 12, 2006 at 19:53 UTC | |
by gabrielle (Novice) on Jan 12, 2006 at 22:42 UTC | |
|
Re: best practices for a complex sort + splitting an alphanumeric string
by jdporter (Paladin) on Jan 12, 2006 at 20:12 UTC | |
|
Re: best practices for a complex sort + splitting an alphanumeric string
by revdiablo (Prior) on Jan 12, 2006 at 20:01 UTC | |
|
Re: best practices for a complex sort + splitting an alphanumeric string
by demerphq (Chancellor) on Jan 12, 2006 at 22:36 UTC | |
|
Re: best practices for a complex sort + splitting an alphanumeric string (natural)
by tye (Sage) on Jan 13, 2006 at 07:30 UTC | |
by gabrielle (Novice) on Jan 13, 2006 at 20:42 UTC |