Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Sort list by position of items in another list

by choroba (Cardinal)
on Feb 18, 2022 at 17:30 UTC ( [id://11141473]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found