Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Sort list by position of items in another list

by johngg (Canon)
on Feb 18, 2022 at 22:10 UTC ( #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 (Sage) 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? | Other CB clients
Other Users?
Others studying the Monastery: (1)
As of 2023-01-29 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?