Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Sort list by position of items in another list

by salva (Canon)
on Feb 18, 2022 at 18:30 UTC ( [id://11141478]=note: print w/replies, xml ) Need Help??


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

my $pchr = "KQRBNP"; my $w1 = "QRKPNBNBQRK"; my $ascii = join '', map chr $_, 0..length($pchr); my $w2 = join '', sort split //, eval "\$w1 =~ y/\Q$pchr\E/\Q$ascii\E/ +r"; eval "\$w2 =~ y/\Q$ascii\E/\Q$pchr\E/"; print "$w1 --[$pchr]--> $w2\n"
Or...
sub mk_sort { my $pchr = shift; my $a = join '', map chr, 0..length(\$pchr); eval qq{sub { (join '', sort split //, (shift =~ y/\Q$pchr\E/\Q$a\ +E/r)) =~ y/\Q$a\E/\Q$pchr\E/r }}; } my $sort = mk_sort "KQRBNP"; my $w1 = "QRKPNBNBQRK"; my $w2 = $sort->($w1); print "$w1 --> $w2\n"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-18 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found