Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 27th, 2001, 06:18 PM
startrader startrader is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 0 startrader User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
sorting...

i need to sort some variables in a textfile... i have three variables per row (int , string ,string), i have tried to sort the rows according to the int variable without success ..
heres an example :

file before sorting:

3 someurl1 sometitle1
2 someurl2 sometitle2
5 someurl3 sometitle3

after sorting the textfile should be

5 someurl3 sometitle3
3 someurl1 sometitle1
2 someurl2 sometitle2

any help would be greatly appreciated
thanks

Reply With Quote
  #2  
Old June 28th, 2001, 12:21 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Talking

TMTOWTDI my friend. But here is a pretty simple method:

Code:
$var1 = "3 someurl1 sometitle1";
$var2 = "2 someurl2 sometitle2";
$var3 = "5 someurl3 sometitle3";

# assign your variables to an array
@vars = ( $var1, $var2, $var3 );

# sort and print
foreach ( reverse sort @vars ) {
     print $_, "\n";
}


This will get your desired output. The key is the reverse before the sort. Without it, you get:
2 someurl2 sometitle2
3 someurl1 sometitle1
5 someurl3 sometitle3

More or less the opposite of what you are looking for. 'sort' by default will sort in ascending order. The reverse will get the desired descending order.
__________________
- dsb -
Perl Guy

Reply With Quote
  #3  
Old June 28th, 2001, 05:08 PM
startrader startrader is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 0 startrader User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
ehm , but let say that i have to sort the following :

$var1 = "3 someurl1 sometitle1";
$var2 = "2550 someurl2 sometitle2";
$var3 = "53 someurl3 sometitle3";

then i got a problem, because the sorting mechanism only looks at the first digit , which means that the output will be:

2550 someurl2 sometitle2
3 someurl1 sometitle1
53 someurl3 sometitle3

regards,
Adam

Reply With Quote
  #4  
Old June 28th, 2001, 05:19 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
You raise a good point .

Well lucky for us, Perl provides some comparison operators( <=>, cmp for numerics and literals respectively ).

Check this:
Code:
$var1 = "3 someurl1 sometitle1";
$var2 = "2 someurl2 sometitle2";
$var3 = "5 someurl3 sometitle3";

@vars = ( $var1, $var2, $var3 );

@sorted = sort sort_desc @vars;

foreach ( @sorted ) {
   print $_, "\n";
}

sub sort_desc {
    $b cmp $a;

}


To sort in ascending order simply reverse the $a and $b variables in the sort_desc routine.

Hope that helps.

Reply With Quote
  #5  
Old June 28th, 2001, 05:49 PM
startrader startrader is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 0 startrader User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks for your help :-} but...when i tested

$var1 = "444 someurl1 sometitle1";
$var2 = "114 someurl2 sometitle2";
$var3 = "44 someurl3 sometitle3";

the output was :

114 someurl2 sometitle2
44 someurl3 sometitle3
444 someurl1 sometitle1


/adam

Reply With Quote
  #6  
Old June 28th, 2001, 06:05 PM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Lightbulb

Ok now I get it .

You want to sort these strings only on the number at the beginning of the line(ok so I think I get it ).

Try this:
Code:
$var1 = "444 someurl1 sometitle1";
$var2 = "114 someurl2 sometitle2";
$var3 = "44 someurl3 sometitle3";

@vars = ( $var1, $var2, $var3 );

%vars;
foreach ( @vars ) {
    $_ =~ m/^(\d+)\s/;
    $vars{$1} = $_;

}

@sorted = sort sort_desc keys %vars;

foreach ( @sorted ) {
   print $vars{$_}, "\n";
}

sub sort_desc {
    $b <=> $a;

}


I'm now grabbing the number a creating a hash, sorting, then printing. That's the short version.

Hope that helps.

Reply With Quote
  #7  
Old July 13th, 2001, 05:48 PM
startrader startrader is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Posts: 0 startrader User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
dsb
thanks for your help

but if i have two or more rows with the same number, then the sort mechanism will only add the last nr and delete the rest(of the same number)
let say that i have :

$var1 = "444 someurl1 sometitle1";
$var2 = "444 someurl2 sometitle2";
$var3 = "44 someurl3 sometitle3";

then the output will be

444 someurl2 sometitle2
44 someurl3 sometitle3

How to do if i want to keep every row ..?
Regards,
Adam

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > sorting...


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway