|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 ![]() |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > sorting... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|