Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Sort list by position of items in another list

by salva (Canon)
on Feb 20, 2022 at 09:35 UTC ( #11141502=note: print w/replies, xml ) Need Help??


in reply to Sort list by position of items in another list

I would say this is a radix sort:
my $pchr = "KQRBNP"; my $w1 = "QRKPNBNBQRK"; my %s; $s{$_}++ for split //, $w1; my $w2 = $pchr =~ s/(.)/$1 x $s{$1}/egr; print "$w1 -> $w2\n"

Replies are listed 'Best First'.
Re^2: Sort list by position of items in another list
by rsFalse (Chaplain) on Feb 20, 2022 at 20:13 UTC
    Similar to yours and choroba's solutions but without string multiplication ('x'):
    my $pchr = "KQRBNP"; my $w1 = "QRKPNBNBQRK"; my $w2 = ""; for my $L ( split //, $pchr ){ $w1 =~ m/$L(?{ $w2 .= $L })(*FAIL)/; } print "$w1 -> $w2\n"
    And a bit similar to drclaw's idea:
    my $pchr = "KQRBNP"; my $w1 = "QRKPNBNBQRK"; my $w2 = ""; my $re = join "|", map { ".*$_" } split //, $pchr; $w1 =~ m/^(?:$re)(?<=(.))(?{ $w2 .= $1 })(*FAIL)/; print "$w1 -> $w2\n"
    Not sure if regex is optimized in some versions and this code fails.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2022-10-05 04:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (20 votes). Check out past polls.

    Notices?