Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
working data sorting

Posted by yamaha102 (yamaha102), 17 March 2008
Sorry to bother you guys again.
I have another problem. I would like to sort (numerical) the input.txt based on the last line and then write to the output.txt.However, i have some difficulties in
(1) How to tie the whole column together so that when i sort the last line the whole column will be moved together?
(2) It's easier way to solve it by  transposing the the input file first and then sort based on column?

Input.txt
Item,A,B,C,D,E,F
m2,1,3,4,6,2,3
pv,3,3,3,4,5,2
ab,2,3,1,8,1,3
m1,1,3,2,9,4,5


Output.txt
Item,A,C,B,E,F,D
m2,1,4,3,2,3,6
pv,3,3,3,5,2,4
ab,2,1,3,1,3,8
m1,1,2,3,4,5,9


for (1)
Code below could only sort a line in text file... i have no idea on how to tie the whole column based on the last line entry.

use Data:umper;
my @data;

$input = 'C:/perl/input.txt';

open(BASE, $input) or die "Can't open $input: $!";
while (<BASE>) {
  chomp;
   push (@data,split (",", $_));
   @newdata = sort {$b <=> $a} @data
}

print Dumper(@newdata);


for (2)
after transpose, probably i could use

my $input  = 'C:/perl/input.txt';
open INPUT,  '<', $input  or die "Cannot open $input: $!";

push (@newdata, map join( ';', @$_ )),
         sort { $a->[2] <=> $b->[2] }
         map [ split /,/ ],
         <IN>;

print Dumper(@newdata);

Please guide.
thank you.

Posted by admin (Graham Ellis), 18 March 2008
Here's a piece of code that sorts the columns in a file based on the last line of the data:

Code:
chomp(@da = <DATA>);
$z=0;
@new_order = map((split(/,/))[1],sort{$a<=>$b}(map($_.",".$z++,split(/,/,$da[-1]))));
print (join(",",(split(/,/))[@new_order]),"\n") foreach (@da) ;

__END__
Item,A,B,C,D,E,F
m2,1,3,4,6,2,3
pv,3,3,3,4,5,2
ab,2,3,1,8,1,3
m1,1,3,2,9,4,5


Results

Code:
dolphin:~ graham$ perl dso
Item,A,C,B,E,F,D
m2,1,4,3,2,3,6
pv,3,3,3,5,2,4
ab,2,1,3,1,3,8
m1,1,2,3,4,5,9
dolphin:~ graham$


Posted by yamaha102 (yamaha102), 18 March 2008
When i see your code.. one thing flash my mind..
Gosh..when am i able to do coding like Graham. where mr  Gharam used 3 lines but i used more han tat and i need to transpose the data and then sort accordingly and finally transpose the data back.

Thanks.

Posted by KevinAD (KevinAD), 18 March 2008
on 03/18/08 at 15:00:06, yamaha102 wrote:
When i see your code.. one thing flash my mind..
Gosh..when am i able to do coding like Graham. where mr  Gharam used 3 lines but i used more han tat and i need to transpose the data and then sort accordingly and finally transpose the data back.

Thanks.


I'm wondering if you understand Grahams code. It's a version of a Schwartzian transform that uses (expressional) syntax instead of  {block} syntax.

Posted by yamaha102 (yamaha102), 30 March 2008
Thanks to teach me the ST.

for the example below i wish to sort the 7th column and follow by the first column.but i faces some problem..

_input.txt__
Item,A,B,C,D,E,F
m2,1,3,4,6,2,2006
po,3,3,3,4,5,2007
ab,2,3,1,8,1,2008
m1,1,3,2,9,4,2008
pv,3,3,3,4,5,2008
pv,3,3,3,4,5,2006



_____________________-
use strict;
use warnings;
use Data:umper;
open (IN, 'input.txt') or die "$!";

<IN>;
my @fields = <IN>;

my @sorted = map  { $_->[0] }
           sort { $a->[6] cmp $b->[6] || $b->[0] <=> $a->[0] }
           map  { [ $_, split/,/] } @fields;

print Dumper (@sorted);

_____________________________

suppose i could get the output as below :


$VAR1 = 'm2,1,3,4,6,2,2006';
$VAR2 = 'po,3,3,3,4,5,2007
';
$VAR3 = 'pv,3,3,3,4,5,2006';
';
$VAR4 = 'pv,3,3,3,4,5,2008
';
$VAR5 = 'm1,1,3,2,9,4,2008
';
$VAR6 = 'ab,2,3,1,8,1,2008
';


but i get :

$VAR1 = 'pv,3,3,3,4,5,2006';
$VAR2 = 'm2,1,3,4,6,2,2006
';
$VAR3 = 'po,3,3,3,4,5,2007
';
$VAR4 = 'pv,3,3,3,4,5,2008
';
$VAR5 = 'm1,1,3,2,9,4,2008
';
$VAR6 = 'ab,2,3,1,8,1,2008
';



Doesnt PV should be group together ??
Could you tell me where goes wrong?
Thanks.

if you suggest tat sort the first column first and then 7th column... then it become as below( wish i dont wan)
$VAR1 = 'pv,3,3,3,4,5,2006';
$VAR2 = 'pv,3,3,3,4,5,2008
';
$VAR3 = 'po,3,3,3,4,5,2007
';
$VAR4 = 'm2,1,3,4,6,2,2006
';
$VAR5 = 'm1,1,3,2,9,4,2008
';
$VAR6 = 'ab,2,3,1,8,1,2008
';



Posted by admin (Graham Ellis), 30 March 2008
You have your cmp and <=> mixed up.

Use cmp to sort ascibetically - as in column 0 - and <=> to sort numerically - as column 6.

Posted by yamaha102 (yamaha102), 30 March 2008
Thanks. but tat's not the root cause.
Actually i am wondering could St solve the sorting as below:
Date,Item
2001,AB
2002,AA
2002,AB
2003,AC
2003,AA

sort by ST
sort { $a->[0] <=> $b->[0] || $a->[1] <=> $a->[1] }

I could get the datewill be sort first then follow by item which output as below:
Date,Item
2001,AB
2002,AA
2002,AB
2003,AA
2003,AC


but i wish both date  and item could be sort together as below
Date,Item
2001,AB
2002,AB
2002,AA
2003,AA
2003,AC

could the ST sort it?

Thanks.





Posted by KevinAD (KevinAD), 31 March 2008
instead of:

sort { $a->[0] <=> $b->[0] || $a->[1] <=> $a->[1] }  

try :

sort { $a->[0] <=> $b->[0] || $b->[1] cmp $a->[1] }  

sorts first field ascending (numeric) and second field descending (alpha);


Note: in your code the second expression uses $a on both sides of the <=> operator, which is of course not correct.



Posted by yamaha102 (yamaha102), 20 April 2008
Fist of all,
Sorry as taking a very long time to reply and say thanks.

Sorry again as my question is not clear.


RawData:
Date,Item
2001,AB
2002,AA
2002,AB
2003,AC
2003,AA


if using ST to sort  (using sort { $a->[0] <=> $b->[0] || $b->[1] cmp $a->[1] }  )
i will getthe output as
Date,Item
2001,AB
2002,AA
2002,AB
2003,AA
2003,AC


BUT i wish sort Date and then Item and then back to sort date and Item ... as below:
Date,Item
2001,AB
2002,AB
2002,AA
2003,AA
2003,AC

________


is it  this kind of sorting could be done?
Thanks. Hopefully my explanation is clearer.

Posted by KevinAD (KevinAD), 21 April 2008
Quote:
if using ST to sort  (using sort { $a->[0] <=> $b->[0] || $b->[1] cmp $a->[1] }  )
i will getthe output as
Date,Item  
2001,AB  
2002,AA  
2002,AB  
2003,AA  
2003,AC  


The sort code will not produce the sorted data you posted, it will produce:

2001,AB  
2002,AB  
2002,AA  
2003,AC
2003,AA  

date ascending, Item descending.

The sort you want makes no sense, it is date ascending but items are mixed, some items are ascending and some descending.


2001,AB  
2002,AB  <--descending items
2002,AA  
2003,AA  <-- ascending items
2003,AC

You can't do that with a simple sort routine. Each year would have to be sorted seperately according to what ever sort you want the items to be  sorted in.

Posted by admin (Graham Ellis), 21 April 2008
I think I'm beginning to understand the requirements.

You're looking to produce the earliest year for each pair of letters, sorted by year and within year by letter pair.   Records which repeat a letter pair for a later year should come, in year order, directly after the other records for that same letter pair.  Thus

2001,AB <-- All the AB records, which started in 2001
2002,AB

2002,AA  <- All the AA records which started in 2002
2003,AA

2003,AC  <- All the AC records which started in 2003

I'm not going to code this or suggest how to do it until I'm clear if I have understood the issue.  


Posted by yamaha102 (yamaha102), 21 April 2008
thanks for your all inputs.
It's not possible... maybe there's not such sorting after all rite
SOrry for the trouble.
Thanks for the guidance all this times. Thank.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho