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