http://qs1969.pair.com?node_id=11141485


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

Hi,

What about inverting the search? You already know the order of the characters. Match them as many times as possible against the input string as a regex with each character as an alternative (ie input "QRKPNB" becomes /Q|R|K|P|N|B/ )?

These couple of lines generate the regex based on the input and match the sorted list against it. The joined matches become the (sorted) output string:

my $PCHR="KQRBNP"; my $w1="QRKPNB"; my $w2=join "", $PCHR=~/@{[join "|",split "",$w1]}/g; print "$w1 -> $w2\n";

Hope it might be of use or interest to your application

Ruben