sort based on the own pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort based on the own pattern
# 1  
Old 06-21-2010
sort based on the own pattern

Hi Everyone,

Code:
[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. Smilie

Thanks
# 2  
Old 06-21-2010
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 (<, =, >).
# 3  
Old 06-21-2010
Hi pludi,

for "my @test=("a","b","c","d");", after print, it will be abcd, but my own pattern is "c" is the 1st, "b" is the 2nd, "d" is 3rd, and last "a".

if i just use "cmp" or "<=>", cannot achieve this customized sort pattern.

Thanks
# 4  
Old 06-21-2010
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:
Code:
#!/usr/bin/perl
@test = qw/a b c d/;
print "@test\n";
@sorted = map { $_->[0] }
  sort { $a->[1] cmp $b->[1] }
  map { $a = $_; $a =~ tr/abcd/4213/; [ $_, $a ] } @test;
print "@sorted\n";

This User Gave Thanks to pludi For This Post:
# 5  
Old 06-21-2010
"would be to translate them to numbers, do a numerical comparison,"

Thanks Smilie

---------- Post updated at 07:00 AM ---------- Previous update was at 06:49 AM ----------

Quote:
Originally Posted by pludi
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:
Code:
#!/usr/bin/perl
@test = qw/a b c d/;
print "@test\n";
@sorted = map { $_->[0] }
  sort { $a->[1] cmp $b->[1] }
  map { $a = $_; $a =~ tr/abcd/4213/; [ $_, $a ] } @test;
print "@sorted\n";

What about if @test = qw/aaa bb 44 BB9/, the output sequence is "BB9 bb aaa 44"? i tried using the code yours, but cannot achieve.

Thanks
# 6  
Old 06-21-2010
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.
# 7  
Old 06-21-2010
hi pludi,

i tried on sort and map, tried to want to replace each element with number, but failed. I used one way which is very unefficient to do it.

Code:
[root@P1Portal ]# cat 1.pl
#!/usr/bin/perl
use strict;
use warnings;

my @test = qw/aaa bb 44 BB9/;
my @new;
foreach (@test) {
        if ($_ =~ m/^(BB9)/i ) {
                $new[0] = $_;
        } elsif ($_ =~ m/^(bb)/i ) {
                $new[1] = $_;
        } elsif ($_ =~ m/^(aaa)/i ) {
                $new[2] = $_;
        } elsif ($_ =~ m/^(44)/i ) {
                $new[3] = $_;
        }
}
print @new;

exit;
#my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { $a = $_; $a = splice(@test,0,4,'4','3','2','1'); [ $_, $a ] } @test;
#print "@sorted\n";

[root@P1Portal ]# ./1.pl
BB9bbaaa44

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort based on one column

Hi All , I am having an input file like this Input file 7 sks/jsjssj/ddjd/hjdjd/hdhd/Q 10 0.5 13 dkdkd/djdjd/djdjd/djd/QB 01 0.5 ldld/dkd/jdf/fjfjf/fjf/Q 0.5 10 sjs/jsdd/djdkd/dhd/Q 01 0.5 21 kdkd/djdd/djdd/jdd/djd/QB 01 0.5 dkdld/djdjd/djd/Q 01 0.5 ... (9 Replies)
Discussion started by: kshitij
9 Replies

2. UNIX for Beginners Questions & Answers

Any way to sort ps output based on STIME?

Hi, This is one of the thing that am looking for when I post the question on the ps wrapper. It has since been closed as it has taken me too long to post an example. I have replaced some of the original content of the ps output. uname -a = SunOS <hostname> 5.11 11.3 sun4v sparc sun4v ... (1 Reply)
Discussion started by: newbie_01
1 Replies

3. Shell Programming and Scripting

Sort based on certain value in a column

Hi, i need to sort content of files based on a specific value. An example as below. Input1.txt Col_1 SW_MH2_ST ST_F72_9S SW_MH3_S6 Col_2 SW_MH3_AS7 ST_S15_9CH SW_MH3_AS8 SW_MH3_ST Col_3 ST_M93_SZ ST_C16_TC (12 Replies)
Discussion started by: redse171
12 Replies

4. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

5. Shell Programming and Scripting

Sort based on numbers

I have a file which has the following data :- how can I sort the data in descending order . My files may have the first column with 1 to 10000 numbers .I need to arrange them in descending order . Thanks (2 Replies)
Discussion started by: lazydev
2 Replies

6. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

7. Shell Programming and Scripting

Sort and extract based on two files

Hi, I am having trouble sorting one file based on another file. I tried the grep -f function and failed. Basically what I have is two files that look like this: File 1 (the list) gh aba for hmm File 2 ( the file that needs to be sorted) aba 2 4 6 7 for 2 4 7 4... (4 Replies)
Discussion started by: phil_heath
4 Replies

8. Shell Programming and Scripting

Sort based on filenames

Hi All, When i give ls -ltr my filenames looks like this: Filename Pattern: Abc_Def_mmddyyyyHHmm.csv $ ls -ltr Jun 05 04:30 Abc_Def_060520111245.csv Jun 05 08:40 Abc_Def_071220121458.csv Jun 06 03:30 Abc_Def_071220111458.csv Jun 06 04:25 Abc_Def_060620110439.csv Jun 07 04:37... (12 Replies)
Discussion started by: HemaV
12 Replies

9. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

10. Shell Programming and Scripting

sort by based on multiple columns

Hi, Is there any way to sort a file in cshell by sort command, sorting it by multiple fields, like to sort it first by the second column and then by the first column. Thanks forhead (1 Reply)
Discussion started by: Takeeshe
1 Replies
Login or Register to Ask a Question