The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-21-2009
dangral dangral is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2002
Posts: 698
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 (permalink)  
Old 04-21-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
show some examples
  #3 (permalink)  
Old 04-21-2009
dangral dangral is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2002
Posts: 698
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 (permalink)  
Old 04-21-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
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 (permalink)  
Old 04-21-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,788
Write your own sort-sub, more in perldoc -f sort
  #6 (permalink)  
Old 04-21-2009
dangral dangral is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2002
Posts: 698
Quote:
Originally Posted by pludi View Post
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 (permalink)  
Old 04-22-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by dangral View Post
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0