Perl: sorting by string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: sorting by string
# 1  
Old 04-21-2009
Perl: sorting by string

I have an array full of string values that need to be sorted, but if a value starts with (regex) 0^[SV] it should be at the beginning of the array. Otherwise the array should be sorted normally using ascii sort.

Please help me create the sub to pass to the sort function.
# 2  
Old 04-21-2009
show some examples
# 3  
Old 04-21-2009
It will look like this before:

Quote:
0L43
0H98
STB45
0S22
0V54
0V72
0S09
EL24
0A13
and after

Quote:
0S09
0S22
0V54
0V72
0A13
0H98
0L43
EL24
STB45
# 4  
Old 04-21-2009
here's one idea
1) got through each element of the array, check for ^0[SV], if yes, push to array1. at the same time, those not ^0[SV] , push to array2
2) sort array1 ( using sort function), and array2
3) join the new arrays together, putting array2 behind array1
# 5  
Old 04-21-2009
Write your own sort-sub, more in perldoc -f sort
# 6  
Old 04-21-2009
Quote:
Originally Posted by pludi
Write your own sort-sub, more in perldoc -f sort
Thanks. I figured it out.

Code:
sub Bysite {
    if ( $a =~ /^0[SV]/ ) {
        return -1;
    }
    elsif ( $b =~ /^0[SV]/ ) {
        return 1;
    }
    lc($a) cmp lc($b);
}

@array = sort Bysite @array;

# 7  
Old 04-22-2009
Quote:
Originally Posted by dangral
Thanks. I figured it out.

Code:
sub Bysite {
    if ( $a =~ /^0[SV]/ ) {
        return -1;
    }
    elsif ( $b =~ /^0[SV]/ ) {
        return 1;
    }
    lc($a) cmp lc($b);
}

@array = sort Bysite @array;

Thats a good solution but it will not sort your sample array into what you posted:

0S09
0S22
0V54
0V72
0A13
0H98
0L43
EL24
STB45

it sorts a little differently:

0S22 <--
0S09 <--
0V54
0V72
0A13
0H98
0L43
EL24
STB45

but maybe close enough is OK Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Sorting Help

Hey guys, I have started to learn perl recently because of a position I took. They want me to master perl and I've been reading books and practicing myself. Basically I,m having my perl script run through a text pad and give the output in a special way e.g input deviceconfig { ... (5 Replies)
Discussion started by: zee3b
5 Replies

2. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

3. Shell Programming and Scripting

string sorting in unix

Hi I need how to sort string characters for Example i have a file that contains this data example string "fan" but i want to display "afn" contained words "afn" is in sorted format for fan. File data faty gafny gaifny dafan gafnniunt O/p gafny gafnniunt (3 Replies)
Discussion started by: polineni
3 Replies

4. Shell Programming and Scripting

[Perl] Sorting an String-Array

Hi, i have a txtfile with the format <Nr>tab<word>tab<other stuff>new line and i want to sort the <word>-colum with a perl script. My textfile: <Nr>tab<word>tab<other stuff>new line 6807 die ART.Acc.Sg.Fem 6426 der ART.Gen.Sg.Fem 2 die ART.Nom.Sg.Fem 87 auf APPR.-- 486 nicht PTKNEG.--... (1 Reply)
Discussion started by: buckelede
1 Replies

5. Shell Programming and Scripting

perl sorting variables

Good morning!! Im trying to practice withe Perl and sorting variables is not going good at all! #!/usr/bin/perl $username = $ENV {'LOGNAME'}; print "Hello, $username\n"; I want to add sort and 'mail' and 'home'. This below is what I have came up with,but of course its not working. ... (5 Replies)
Discussion started by: bigben1220
5 Replies

6. Shell Programming and Scripting

Perl sorting

Hi, I have a file in this format: a b c d e a b c d e a b c d e i need perl script to sort 2nd column in alphabatical order The script i use is #!/usr/bin/perl my @words=<>; foreach(sort mysort @words) { print; (4 Replies)
Discussion started by: Learnerabc
4 Replies

7. Shell Programming and Scripting

Sorting an address string

I'm in an introduction to Unix class and well I'm kind of stuck on one part of the lab for this week or shell scripts. Basically we're given a file named address.data and we're supposed to create a script to sort it according to zip code, last name, and first name (not at the same time of course).... (0 Replies)
Discussion started by: Minimum
0 Replies

8. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
3 Replies

9. Shell Programming and Scripting

perl sorting

I have many files that I need to sort each week. I know how to do in Unix, but for this task it appears best to do native inside an existing perl program. So, simplified, I have a file similar to the following: Joe_________12_Main_St__A001________LX Benny_______5_Spring____A002________LX... (5 Replies)
Discussion started by: joeyg
5 Replies

10. UNIX for Advanced & Expert Users

Sorting a string

Hello all, I have some 20-digit strings - say 1234567890abcdefghij I want to remove the 3rd and then the 11-18th strings, but leave a space between the two resulting strings eg "3 abcdefgh" I only know how to use "cut", does anyone know any way I can do this? All these strings are in the same... (14 Replies)
Discussion started by: Khoomfire
14 Replies
Login or Register to Ask a Question