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

A few quick comments. This code:

if ($oids{ifName}{$a} =~ /\//) { @a = split(/\//, $oids{ifName}{$a}); @b = split(/\//, $oids{ifName}{$b}); } else { @a = $oids{ifName}{$a}; @b = $oids{ifName}{$b}; }

Can be replaced with simply:

@a = split m{/}, $oids{ifName}{$a}; @b = split m{/}, $oids{ifName}{$b};

And this code:

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]); }

Can be replaced with:

if ($a[0] =~ /[A-Za-z]/) { $a[0] cmp $b[0] || $a[1] <=> $b[1]; } else { $a[0] <=> $b[0] || $a[1] <=> $b[1]; }

I'm sure there are other refinements, but these ones stood out to me immediately.

Update: perhaps the 2nd can even be simplified to:

$a[0] <=> $b[0] || $a[0] cmp $b[0] || $a[1] <=> $b[1];

I think so, but I haven't tested to make sure.

Another update: I noticed I left out the 3rd level comparison, but hopefully it should be obvious how to add it in. :-)

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

    Oooh, thanks! I like the reduction of the array assignments. (Reducing my code to elegant/minimal pieces is proving a challenge.)

    (Not annoying at all, ty!)