![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting ASCII string containing numbers | GKnight | UNIX for Dummies Questions & Answers | 8 | 04-24-2009 08:26 AM |
| Sorting an address string | Minimum | Shell Programming and Scripting | 0 | 09-13-2008 04:15 PM |
| PERL data - sorting | eagercyber | Shell Programming and Scripting | 3 | 06-20-2008 03:02 AM |
| perl sorting | joeyg | Shell Programming and Scripting | 5 | 02-15-2008 03:56 PM |
| Sorting a string | Khoomfire | UNIX for Advanced & Expert Users | 14 | 01-18-2006 04:34 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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. |
|
||||
|
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 |
|
|||||
|
Write your own sort-sub, more in perldoc -f sort
|
|
||||
|
Quote:
Code:
sub Bysite {
if ( $a =~ /^0[SV]/ ) {
return -1;
}
elsif ( $b =~ /^0[SV]/ ) {
return 1;
}
lc($a) cmp lc($b);
}
@array = sort Bysite @array;
|
|
||||
|
Quote:
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 ![]() |
| Sponsored Links | ||
|
|