Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Sort list by position of items in another list

by johngg (Canon)
on Feb 18, 2022 at 22:10 UTC ( [id://11141483]=note: print w/replies, xml ) Need Help??


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

How about a hash slice? No sorting required.

johngg@abouriou:~$ perl -Mstrict -Mwarnings -E 'say q{}; my @PCHR = qw{ K Q R B N P }; my $w1 = q{QRKPNB}; my %w1LU = map { $_ => $_ } split m{}, $w1; say join q{}, @w1LU{ @PCHR };' KQRBNP

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Sort list by position of items in another list
by LanX (Saint) on Feb 19, 2022 at 02:28 UTC
    I see multiple limitations:
    • you can't sort lists with repeated elements in $w1
    • the slice will produce undef for every order-element from PCHR missing in the list
    • list-elements without order won't show up

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2025-11-11 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (68 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.