Closed Thread Icon

Topic awaiting preservation: Sorting flat text files (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=11969" title="Pages that link to Topic awaiting preservation: Sorting flat text files (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Sorting flat text files <span class="small">(Page 1 of 1)</span>\

 
butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-27-2001 21:12

I have a flat text file delimited with pipes that looks like this:

3/3/01<B>

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 12-27-2001 21:28

Read all lines from your text file into array, write function that compares two date items, use usort() function to sort array using your date comparing function...


butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-27-2001 21:43

Thanks for the start mr.maX!


butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-28-2001 03:31

Okay, after mr.maX pointed me in the right direction, and after spending some time in the online PHP manual (I love that thing), I have come up with this code. Whats even more amazing is... IT WORKS! I'm just not sure why. I know I've made $my_array a two dimentional array, which is why I'm having trouble understanding why this works. What makes the sort function sort on the date field which would be $my_array[0][0], and $my_array[1][1], ect. if my thinking is correct.

Could someone please enlighten me?

Here's the code in question:

<?
$my_array = array();
$fp = fopen("my_file.txt", "r");
while (!feof($fp))
{
$row = fgets($fp, 1000);
$fields = explode("

jiblet
Paranoid (IV) Inmate

From: Minneapolis, MN, USA
Insane since: May 2000

posted posted 12-28-2001 05:12

$my_array is an array of arrays (each of which contains one row's worth of values)

$my_array[0] is the array that holds the values of the first row.

$my_array[0][1] is the scalar that is the second value of the first row in the text file.

So basically it is $my_array[row][column].

When you call sort($my_array) it is basically trying to put the rows in order, and apparently it does that by looking at the first value.

if you had done sort($my_array[0]) it would have put the columns for that row in order instead.




-jiblet

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 12-28-2001 09:37

Actually, I haven't suggested that you use sort() function, Butcher And there's one simple reason why - it won't sort dates correctly. It sorts array using simple alphanumeric comparing routine. If your text file contained entries for the next year, it wouldn't be sorted correctly. For example put the following in your text file and you'll see:

3/13/01

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-28-2001 12:48

Thanks mr.maX

I didn't start with usort() as you suggested because I was just trying to see what I was getting returned so I could set things right in my mind to try and move forward. Then it worked so I wasn't sure if I needed to do any more. I'm not sure I could have come up with the proper sort function on a bet anyway.

Thanks again.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 12-28-2001 22:53

I'm sorry jiblet,

I didn't want you to think I didn't appreciate your answer. I got so wrapped up in trying to figure out how the function that mr.maX wrote for me worked, that I forgot to thank you for your help.

So...

Thanks jiblet!!!

I appreciate your help.

-Butcher-



[This message has been edited by butcher (edited 12-28-2001).]

« BackwardsOnwards »

Show Forum Drop Down Menu