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.


DWIM is Perl's answer to Gödel

In reply to Pimp my code - Schwartzian transform maybe? by GrandFather

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.