[root@]# cat 1.pl
#!/usr/bin/perl
use strict;
use warnings;
my @test=("a","b","c","d");
print @test;
[root@]# ./1.pl
abcd
if i want to sort the @test, based on my own partten, the output is "cbda", how to do? as i know perl sort i can use cmp and <=>, but how to do with my own sort patten.
The first thing you should define is what your pattern is. Otherwise, just pass a code block or sub-reference as the first argument to sort, remember that the left- and right-hand variables are always named $a and $b, and return one of -1,0,1 (<, =, >).
What about d-z, A-Z, 0-9, ...? What about values with more than 1 character?
One possible approach, if it's only a few possible characters (=< 10), would be to translate them to numbers, do a numerical comparison, and translate them back after sorting, eg:
The sort function uses code just like any regular sub. So if you can write the code to do single comparisons giving the right results, you can plug it into the sort function without problems.
If you're doing this to learn Perl, read the code I've given, plus perldoc -f sort and perldoc -f map, and then ask specific questions.
If you need a generic sorting mechanism, give us more details on the required order, covering every possible valid input.
Well, that sort order seems pretty much arbitrary. Unless you can come up with a concise description of the algorithm you won't get any better results than what you already have.