Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I read txt file and sort the array based on column X content? 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have the following problem.-
flat file looks like this:
0 1 2 3 4 5 6 7 8 <- not in file, just for this illustration
A~B~C~D~0~A~2~B~X

I can easily use file() to read file into an array and use sort()to sort the array.

Here is the kicker, sort() will work based on column 0 and I need to be able to sort on any of the columns.

Here is my idea:
1. Load file to array1
2. Place sort column as column 0 shifting the array 1 column
3. Sort the newly altered array

I am afraid of the time this process may take and was hoping one you guys/gals would have a better idea.

Thank you all in advance for your time!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

SouthBeach said:
2. Place sort column as column 0 shifting the array 1 column
In case the lines are unique, you could use them as keys :
Code:
$array=file("sort.me");
$column=0;
foreach ($array as $str) {
  $field=split(" ",$str);
  $sortable[$str]=$field[$column];
}
[url=http://php.net/asort/]asort[/url]($sortable);
$array=array_keys($sortable);
unset($sortable);
Otherwise you will have to use a user defined callback function :
Code:
$array=file("sort.me");
$column=0;
[url=http://php.net/usort/]usort[/url]($array,"mysort");

function mysort($a,$b)
{
  global $column;
  if ($a==$b) return 0;
  $afield=split(" ",$a);
  $bfield=split(" ",$b);
  return $afield[$column]<$bfield[$column]?-1:1;
}
Note that is recommended to implement some caching for the [tt]split()[/tt]'s result to not execute it again and again.

Feherke.
 
Check out usort() which allows you to supply a function which does the actual compare. So it can do anything it needs to do including using a column other than 0 as you have total control over what happens.
What will hapen is the hook (or callback) you supply will get given two values, you compare this in what ever way you see fit and return the correct flag.
It's easier to look at the manual that for me to describe !
Any issues come back
 
ingressman, you are pointing me in the same direction as feherke ...

Funny thing, I checked the samples on the link ingressman provided and compared to the code feherke posted and there is no comparison, feherke's code is simpler, cleaner and I actually understand it - Cannot say same about the samples on the php.net page. :)

Thank you both!!!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
yes sorry didn't read all the code ,just saw the asort() call !
 
Hi

Ok, then let us add the above mentioned caching too :
Code:
$array=file("sort.me");
$column=0;
[red]foreach ($array as $str) {
  $field=split(" ",$str);
  $temp[$str]=$field[$column];
}[/red]
usort($array,"mysort");
[red]unset($temp);[/red]

function mysort($a,$b)
{
  global [red]$temp[/red];
  if ($a==$b) return 0;
  return [red]$temp[$a][/red]<[red]$temp[$b][/red]?-1:1;
}
This way the callback function is as fast as possible.

( Do not be confused by the similarity between building the $temp array, and building the $sortable array in my [tt]asort()[/tt] example. There I wrote that the method is applicable only if the lines are unique. But here is no such restriction. That is because in the [tt]asort()[/tt] example $sortable was used to store the data, but here is used to store only the comparable parts. )

Still understandable ?

Feherke.
 
feherke,

Where does the function myfunction($a,$b) get the value for $b? I can clearly see $a ($array), but $b is meant to be the column number or array element we are targeting ... Where is this value coming from?

FYI: It works fine, but this is the one thing I do not get!

Thanks!!!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

SouthBeach said:
Where is this value coming from?
From the documentation :
[URL unfurl="true" said:
http://php.net/usort/[/URL]]The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
You are advancing too fast with reading the documentation and are mixing up the knowledge. In different circumstances the callback functions are receiving different parameters.
SouthBeach said:
I can clearly see $a ($array), but $b is meant to be the column number or array element we are targeting ...
I think you took that from the [tt]array_walk()[/tt] function. Because of different functionality, that passes different parameters to the callback function.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top