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

Interestingly, the Python code fails if w1 contains characters outside of PCHR. Therefore, using only valid characters the following should work:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @pchr = qw( K Q R B N P ); my $w1 = 'QRKPNBQ'; # <- Q is repeated! my $w2 = join "", map $_ x (() = $w1 =~ /$_/g), @pchr; say "$w1 \-> $w2";
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Sort list by position of items in another list
by gflohr (Acolyte) on Feb 18, 2022 at 18:19 UTC
    Cool! And, yes, it is checked elsewhere that the string only contains "valid" characters.