Alphabetical text file sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Alphabetical text file sorting

Post by spacebiscuit »

Hi,

I need to add strings of text to a text file in alphabetical order. I'll provide a form for a user to input the text then once submitted the string will be inserted into the file in order.

Each line with have only one string, so 10 strings will mean 10 lines.

Are there any functions for sorting aplhabetically? Any suggestion would be appreciated.

Thanks,

Rob.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

sort() should do it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

make sure that you turn it into a array first though because sort() wont work on a string. so if there is a text field where the person inputs words seperated by a space do somtin like

Code: Select all

$words = $_POST['words'];

$words = trim($words);

$words  = explode(' ', $words);

$words = sort($words);

//$words is now alphabetically sorted, to write to a txt file...

$open = fopen('file.txt', 'a');//note, this will make file length 0, check out fopen in the manual

foreach($words as $val)
{
    fwrite($val, $open);
}

fclose($open);
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Hi,

I have tried a few things but with little joy.

I am reading the contents of a text file to an array:

Code: Select all

$list=file("email_list.txt");
Then I am adding the new input text to the last index of the array:

Code: Select all

$x=count($list);

$x++;

$list[$x]=test_string;
Then sorting the array:

Code: Select all

$list=sort($list);
Then testing the output:

Code: Select all

for($y=0; $y<$x; $y++){
	echo"$y: $list[$y]<br>";
                      }
However when I add the string to the last index it doesn't seem to write to the array and then after sorting all the indexes seem to be empty.

I don't think I am far of solving this, any ideas guys?

Thanks,

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

take out the post-icrement line, it's not needed.

Code: Select all

$list[] = 'some string';
will append to the array.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Ok this is very odd, I have stripped the code down to bare basics, it seems that the sort function is deleting the array index values:

Code: Select all

<?

$list=file("email_list.txt");

$x=count($list);

for($y=0; $y<$x; $y++){
	echo"$y: $list[$y]<br>";
                      }

$list=sort($list);

for($y=0; $y<$x; $y++){
	echo"$y: $list[$y]<br>";
                      }

?>
If you run the code, before sorting the data is output, after sorting nothing is output!

Any ideas?

Thanks,

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the array is re-indexed when sort() runs.. if the indexes are off, they will be rearranged to be within alignment (sequencial)
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

Ok so what does that mean and how does it apply to my case?

Thanks,

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

print_r() the array before and after, you'll see..
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

You can see the output here:

http://www.robburne.f2s.com/email/

Although this doesn't help just confirms what I am saying, the osrting seems to be deleting any data in the array indexes.

How can I sort the array and keep the data contained within.

Thanks,

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops.. I forgot that sort() does not return the new array. It directly modifies the existing one. So don't assign the return from sort() to anything.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

This should work:

Code: Select all

<?
$list = file("email_list.txt");

//output before sort
foreach($list as $index=>$value)
{
  echo "$index: $value<br />";
}

//add your new entries here

sort($list);

//output after sort
foreach($list as $index=>$value)
{
  echo "$index: $value<br />";
}
?>
Kind of off topic, but look into foreach loops - they're much nicer than using the for loop you had.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Post by spacebiscuit »

This is possible the most frustrating piece of code I have ever worked on.

This is where I am at so far, an old file is opened for read/write. The file contains 3 lines - z,m and a. A variable $email is written to the end of the file. So if say the variable was 'c' the old file now reads as follows in notepad.

z
m
a
c

Code: Select all

$handle=fopen("email_list.txt","a+");

fwrite($handle, "$email");

fclose($handle);
All good so, now onto sorting.

Code: Select all

$list=file("email_list.txt");

sort($list);
Now onto writting the sorted array to a new file:

Code: Select all

$handle2=fopen("email_list2.txt","x+");

   foreach($list as $val)
   {
      fwrite($handle2, $val, strlen($val)); 
   }

fclose($handle2);
All appears to be ok , but when I look at the contents of the file I have:

a
cm
z

The order is correct but as you can see there is no carriage return after the inserted variable. It seems as if I somehow have to write a carriage return to the end of the file after adding the last line. I have tried with \n \r etc but all that seems to do is write a odd square charater to the file.

I am sure this is something simple, any idea please guys?

Thanks,

Rob.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

//.......

// load the file, strip out surrounding whitespace and remove any blank or newly blank lines
$list = array_filter(array_map('trim',file($filename),'strlen');

$data = implode("\r\n", $list);


$fp = fopen($newFilename,'w');
fwrite($fp, $data);
fclose($fp);
adapt as needed..
Post Reply