Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Create sort function from a text file

by haukex (Archbishop)
on Aug 16, 2021 at 13:04 UTC ( [id://11135880]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Create sort function from a text file
in thread Create sort function from a text file

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.)

Replies are listed 'Best First'.
Re^4: Create sort function from a text file
by jdporter (Paladin) on Aug 19, 2021 at 02:55 UTC
    a Schwartzian transform could also be used to improve performance

    Even faster: the Guttman Rosler Transform. (But check out the links at the bottom of that article for even more performance enhancement.)

        Thanks for these links (++). Very interesting.

        that GRT is tricky is shown by this necropost (by me!) pointing out a bug that lay undetected for 18 months in a node with 264 rep!

        In reading through what I presume to be the original GRT paper I was directed to Sort::Maker by Uri Guttman himself. It appears that this module is designed to help abstract away such trickiness and would likely be the approach I would take for such tasks in future. YMMV, of course.


        🦛

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135880]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found