Forum Moderators: coopster

Message Too Old, No Replies

Sort a text file

         

sumeet ruiwale

10:30 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



Hi,
i need a script which will help me sort a file based on year and date. like the file contians enteries 05/10/2005 -- user 1 , 03/2/2005 - user 2 , 01/1/2006 - user 3. So i need the script to sort this file in asscending order based on the date and year. can anyone help me with this. i am having problems using the sort function. doesnt seem to work for a file. do i hae to put in an array or something?

help

coopster

10:32 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, sumeet ruiwale.

First, is that date column defined as DATE? Or do you have it defined as CHAR or otherwise? Defining it as a DATE type column will make your life much, much easier.

sumeet ruiwale

10:36 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



if u want i can send u the log file, so u can understand what kinda file i want is to sorted.

coopster

10:47 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm sorry, I was thinking you had this in a database table. I'm assuming now that you are referring to a flat text file. Is that correct?

sumeet ruiwale

10:47 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



$data = file("paultesting.txt");
foreach ($data as $line)
{ print $line; }

$data = file("paultesting.txt");
sort($data);
print "After sorting:\n";

foreach ($data as $line)
{ print $line; }
this is the code i have so ffar. but its not printing the sorted file as the output. any ideas

sumeet ruiwale

10:49 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



yes its a flat text file. no data base

coopster

10:53 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have a look at this thread, it is very similar to what you want to do ...

[webmasterworld.com...]

sumeet ruiwale

10:59 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



so basically i should use usort instead of sort. but is the rest of code correct. god php is hard!1

sumeet ruiwale

11:19 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



there should be no user input. i want is the script to read the file and sort it accoridng to date and year automatically. after sorting the file, if the file contains month may to june in year 2005, the script should automatically find out which weeks of the year are in the file and give output as .. week 20 - users 20 , week 21 - users 21. no user input, all as to done by the script.

the log file look like this ...

05/09/2005 11:59 AM 720 perwin.cmd
05/09/2005 06:34 PM 122 mwymore.cmd
05/10/2005 10:11 AM 116 erik.cmd
05/10/2005 10:18 AM 116 tung.cmd
05/10/2005 10:24 AM 122 dhodges.cmd
05/10/2005 10:42 AM 120 vipulb.cmd
05/10/2005 10:57 AM 122 tderose.cmd
05/10/2005 11:17 AM 118 donby.cmd
05/10/2005 01:33 PM 120 raviup.cmd
05/10/2005 02:11 PM 120 shelly.cmd
05/10/2005 02:22 PM 295 balar.cmd
05/10/2005 02:27 PM 124 cantrell.cmd
05/10/2005 02:52 PM 124 tserrata.cmd
05/10/2005 03:18 PM 122 rpotral.cmd
05/10/2005 04:39 PM 120 ramkib.cmd
05/11/2005 06:44 AM 120 giulia.cmd
05/11/2005 11:13 AM 124 varghese.cmd
05/11/2005 11:20 AM 124 rajasree.cmd
05/11/2005 05:01 PM 124 jessicaz.cmd
05/05/2005 02:00 PM 58 joel.cmd
05/05/2005 02:39 PM 120 pentek.cmd
05/06/2005 09:20 AM 124 ramkumar.cmd
05/06/2005 09:28 AM 122 wctseng.cmd
05/06/2005 10:09 AM 60 bboike.cmd
05/06/2005 10:13 AM 122 susanhu.cmd
05/06/2005 10:23 AM 122 shankha.cmd
05/06/2005 10:44 AM 120 scotts.cmd
05/06/2005 10:52 AM 122 rajeshk.cmd
05/06/2005 11:04 AM 120 mnchua.cmd
05/06/2005 11:09 AM 118 jsjou.cmd
05/06/2005 11:37 AM 118 zwong.cmd
05/06/2005 11:47 AM 124 kwanthai.cmd
05/06/2005 12:03 PM 122 kjensen.cmd
05/06/2005 12:56 PM 116 yong.cmd
05/06/2005 03:01 PM 122 oropeza.cmd
05/06/2005 04:26 PM 118 ncaro.cmd
05/12/2005 03:07 PM 124 plnguyen.cmd
05/12/2005 06:08 PM 114 rau.cmd
05/12/2005 11:03 PM 124 krithika.cmd
05/13/2005 07:50 AM 124 michaelt.cmd
05/13/2005 08:06 AM 122 amitabh.cmd

so it should 1st sort it my date and year and then for the for like year 2005, what ever weeks r in the file, it should give output like if the dates fall in week 23 of the year 2005 it should give week 23 = number of user 20 etc..

can anyone help me adhieve this thing. god this seems too hard for a begineer of php like me.

coopster

12:03 am on Jul 1, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You'll never get past the beginner stage if you don't take some initiative ;)

OK, I'll help you over this hump, but you have to read through this code snippet and understand how it works...

function cmp($a, $b) { 
// This will make it sort on the first index of the array;
// remember, arrays start with zero (0)!
$column_to_sort_on = 0;
$datea = explode('/', $a[$column_to_sort_on], 3);
$datea = $datea[1] . '/' . $datea[2] . '/' . $datea[0];
$dateb = explode('/', $b[$column_to_sort_on], 3);
$dateb = $dateb[1] . '/' . $dateb[2] . '/' . $dateb[0];
if ($datea == $dateb) return 0;
return ($datea < $dateb)? -1 : 1;
}
// Read the file, print a before snapshot, sort, print after snapshot:
// READ FILE IN:
$filename = "paultesting.txt";
$id = fopen($filename, "r");
while ($data = fgetcsv($id, filesize($filename)," "))
$table[] = $data;
fclose($id);
// BEFORE:
$before = "<table>\n";
foreach($table as $row) {
$before .= "<tr>";
foreach($row as $data) {
$before .= "<td>$data</td>";
}
$before .= "</tr>\n";
}
$before .= "</table>\n";
// SORT:
usort($table, "cmp");
// AFTER:
$after = "<table>\n";
foreach($table as $row) {
$after .= "<tr>";
foreach($row as $data) {
$after .= "<td>$data</td>";
}
$after .= "</tr>\n";
}
$after .= "</table>\n";
?>
<html><head><title>usort</title></head>
<body>
<h1>usort</h1>
<h4>Before:</h4>
<?php print $before;?>
<h4>After:</h4>
<?php print $after;?>
</body>
</html>

maxi million

4:17 am on Jul 1, 2005 (gmt 0)

10+ Year Member



Works great! that script is going to my library :)