GrandFather has asked for the wisdom of the Perl Monks concerning the following question:
The code below seems a bit clunky to me. It is generating the correct result. It's unlikely to be used with a huge list of tagIndexes, but the list could easily be a few hundred entrys long.
Part of cleaning it up is likely to involve a Schwartzian transform, but the flagValue sub seems clunky to me too. Any ideas for cleaning it up?#!/usr/bin/perl use warnings; use strict; my %tagTypes = ( code => ['code', 'code', ''], bold => ['b', 'Bold', 'F'], teletype => ['tt', 'Teletype text', 'F'], id => ['link id://', 'Node id link', 'L'], readmore => ['readmore', 'Readmore', 'RB'], readmoretitle => ['readmoretitle', 'Readmore Title', 'RTB'], ); my @tagIndexes = ( ['on', 'code', '1.0'], ['off', 'code', '3.6'], ['on', 'id', '3.9'], ['on', 'bold', '3.9'], ['off', 'id', '3.20'], ['off', 'bold', '3.20'], ); print join "\n", map {"$_->[0] $_->[1] $_->[2]"} sort modeOrder @tagIn +dexes; sub modeOrder { # Sort compare routine to order tags by markup generation order my ($aType, $aItem, $aIndex) = @$a; my ($bType, $bItem, $bIndex) = @$b; return $_ if ($_ = int ($aIndex) <=> int ($bIndex)); $aIndex =~ s/\d*\.//; $bIndex =~ s/\d*\.//; return $_ if $_ = $aIndex <=> $bIndex; return $_ if $_ = $aType cmp $bType; my $aMode = ${$tagTypes{$aItem}}[2]; my $bMode = ${$tagTypes{$bItem}}[2]; return flagValue ($aMode) <=> flagValue ($bMode) if $aType eq 'on' +; return flagValue ($bMode) <=> flagValue ($aMode); # off case } sub flagValue { my $flags = shift; return 1 if -1 != index $flags, 'R'; return 2 if -1 != index $flags, 'T'; return 3 if -1 != index $flags, 'B'; return 4 if -1 != index $flags, 'I'; return 5 if -1 != index $flags, 'F'; return 6 if -1 != index $flags, 'L'; return 7; }
Prints:
on code 1.0 off code 3.6 on bold 3.9 on id 3.9 off id 3.20 off bold 3.20
Update: So far sub flagValue hasn't been addressed. The intent of the routine is to priority encode a sort value given a string that may contain multiple flags. For example a flag string may be 'BFXCU' which should encode as 3 using the original code.
Note that the actual values used are unimportant except that it would be good if the sort order were the same regardless of using <=> or cmp. The sort order must remain the same as the original code.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pimp my code - Schwartzian transform maybe?
by bart (Canon) on May 06, 2006 at 11:52 UTC | |
|
Re: Pimp my code - Schwartzian transform maybe?
by salva (Canon) on May 06, 2006 at 13:36 UTC | |
by salva (Canon) on May 07, 2006 at 13:14 UTC | |
|
Re: Pimp my code - Schwartzian transform maybe?
by ikegami (Patriarch) on May 06, 2006 at 20:13 UTC | |
|
Re: Pimp my code - Schwartzian transform maybe?
by GrandFather (Saint) on May 07, 2006 at 11:38 UTC | |
|
Re: Pimp my code - Schwartzian transform maybe?
by wfsp (Abbot) on May 07, 2006 at 12:21 UTC | |
by GrandFather (Saint) on May 08, 2006 at 12:05 UTC |