I have a web interface where users can manage their own population of STB hardware. ... What I want to do is allow users to build their own custom sort statements which they can store in a text file.

This means you'd be giving your users the power to run arbitrary Perl code under whatever user the webserver executes scripts as. So for example, if your web interface has access to a database, you're giving your users the power to access anything in that database that the web interface can, likely including other customer's records. Here you said:

I maintain all instances of the interface

One safer alternative is to give your users a predefined selection of sort orderings. Regexes such as the ones below cover all the cases you showed here. If some other user has yet another custom naming scheme that these orderings don't match, or they want some other arbitrary sort order, then it'd be fairly straightforward for you to add a new set of regexes to the %orderings hash. You would remain in control of the code that gets executed.

use warnings; use strict; my @examples = ( [ 'Rack1-Unit2', 'Rack3-Unit1', 'Rack1-Unit1' ], [ 'R2U3', 'R1U4', 'R10U1', 'R1U1' ], [ 'R2-U3', 'R1-U4', 'R10-U1', 'R1-U1' ], ); my $rackre = qr/R(?:ack)?(\d+)/i; my $unitre = qr/U(?:nit)?(\d+)/i; my %orderings = ( rackfirst => sub { ($a =~ $rackre)[0] <=> ($b =~ $rackre)[0] or ($a =~ $unitre)[0] <=> ($b =~ $unitre)[0] }, unitfirst => sub { ($a =~ $unitre)[0] <=> ($b =~ $unitre)[0] or ($a =~ $rackre)[0] <=> ($b =~ $rackre)[0] }, ); for my $ex (@examples) { print "Input: @$ex\n"; for my $o (sort keys %orderings) { my @sorted = sort {&{$orderings{$o}}} @$ex; print "$o: @sorted\n"; } } __END__ Input: Rack1-Unit2 Rack3-Unit1 Rack1-Unit1 rackfirst: Rack1-Unit1 Rack1-Unit2 Rack3-Unit1 unitfirst: Rack1-Unit1 Rack3-Unit1 Rack1-Unit2 Input: R2U3 R1U4 R10U1 R1U1 rackfirst: R1U1 R1U4 R2U3 R10U1 unitfirst: R1U1 R10U1 R2U3 R1U4 Input: R2-U3 R1-U4 R10-U1 R1-U1 rackfirst: R1-U1 R1-U4 R2-U3 R10-U1 unitfirst: R1-U1 R10-U1 R2-U3 R1-U4

(Note a Schwartzian transform could also be used to improve performance.)


In reply to Re^3: Create sort function from a text file by haukex
in thread Create sort function from a text file by Doozer

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.