Yes you can use the Schwartzian Transform. What you must do then, is preprocess the data so you no longer have to do those things in the comparison sub. I'm talking about stuff like int ($aIndex), $aIndex =~ s/\d*\.//, $aMode = ${$tagTypes{$aItem}}[2] and flagValue ($aMode). Each could be just a precaluted item in an anonymous array.

Preprocessing these expressions can be done with code like this:

map { my ($type, $item, $index) = @$_; my($fract) = $index =~ /\.(\d+)/; my $mode = ${$tagTypes{$item}}[2]; my $flagValue = flagValue($mode); [ $_, # original, 0 $type, # 1 int($index), # 2 $fract, # 3 $mode, # 4 $flagValue, # 5 ]; } @tagIndexes
The complete Schwartzian Transformed code then looks like this:
print join "\n", map {"$_->[0] $_->[1] $_->[2]"} map $_->[0], sort { $a->[2] <=> $b->[2] # int || $a->[3] <=> $b->[3] # fract || $a->[1] cmp $b->[1] # type || ( $a->[1] eq 'on' ? $a->[5] <=> $b->[5] # flagValue : $b->[5] <=> $a->[5] ); # off case } map { my ($type, $item, $index) = @$_; my($fract) = $index =~ /\.(\d+)/; my $mode = ${$tagTypes{$item}}[2]; my $flagValue = flagValue($mode); [ $_, $type, int($index), $fract, $mode, $flagValue ]; } @tagIndexes;

It appears to produce the same outcome for me.


In reply to Re: Pimp my code - Schwartzian transform maybe? by bart
in thread 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.