Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Sort list by position of items in another list

by gflohr (Acolyte)
on Feb 18, 2022 at 15:47 UTC ( [id://11141469]=perlquestion: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
      PCHR = ["K", "Q", "R", "B", "N", "P"]
      w1 = "QRKPNB"
      w2 = "".join(sorted(w1, key=PCHR.index)) # Translate this to Perl ..
    +.
      print(w1 + " -> " + w2)
    
  2. or download this
      my $i = 0;
      use constant PCHR => split //, 'KQRBNP';
    ...
      my $w1 = 'QRKPNB';
      my $w2 = join '', sort { $pchr{$a} <=> $pchr{$b} } split //, $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: perlquestion [id://11141469]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found