#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11141469 use warnings; use List::AllUtils qw( nsort_by ); my $pchr = 'KQRBNP'; my $w1 = 'QRKPNB'; my $w2 = join '', nsort_by { index $pchr, $_ } split //, $w1; print "Given pchr = $pchr, then $w1 -> $w2\n";