473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to sort the sum of columns in a delimited txt flat file?

Hello!

I am stuck with this. Can anyone help me?

I have some rows in a text file. That I need: sum from selected columns and then sort the rows depending on the sum result and print the 10 larger sum results.

I have a part in this incomplete script that works but I cant put the array in this calculation. After this I try something else but there I need to sort the rows of it (depending on the sum of some columns that is being echoed.

Thanks in advance!!

data.txt is the txt that contains the records and here is the code of php:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. function topTenDistricts($arr, $len) {
  5.  
  6. for ($i=0; $i < $len; $i++) {
  7.     for ($j=0; $j < $len-1; $j++) {
  8.         if ($arr[$j+1] > $arr[$j]) {
  9.             $temp = $arr[$j];
  10.             $arr[$j] = $arr[$j+1];
  11.             $arr[$j+1] = $temp;
  12.         }
  13.     }
  14. }
  15. for ($k=0 ; $k < $len; $k++)
  16. print($arr[$k] . "\n");
  17.  
  18. }
  19.  
  20. //// example of previous array sorting ->> topTenDistricts($mydata, 10);
  21. //$mydata = array();
  22. //$mydata[0] = 12;
  23. //$mydata[1] = 4;
  24. //$mydata[2] = 44;
  25. //$mydata[3] = 3;
  26. //$mydata[4] = 6;
  27. //$mydata[5] = 32;
  28.  
  29. $max = 0;
  30. $file = file("data.txt");
  31. $r=0;
  32. $sum=array();
  33. foreach ($file as $line){
  34.     list($row[$r]['partno'], $row[$r]['title'], $row[$r]['color'], $row[$r]['width'], $row[$r]['length'], $row[$r]['height'], $row[$r]['time'], $row[$r]['price'], $row[$r]['place']) = split(";", $line);
  35.     $sum[r]=$row[$r]['color'] + $row[$r]['width'] + $row[$r]['length'] + $row[$r]['height'] + $row[$r]['time'] + $row[$r]['price'] + $row[$r]['place'];
  36.     //echo 'Name DD. : '.$row[$r]['title'].' | Sum : '.$sum.'<br>';
  37.     echo 'Name DD. :'.$row[$r]['title'].'with :'.$sum[r].' records<br>';
  38.     $r++;
  39.     if ($sum >= $max) {
  40.                 $max = $sum;
  41.                 $title = $row[$r]['title'];        
  42.                 }
  43.     }
  44.  
  45.  
  46.  
Attached Files
File Type: txt data.txt (2.0 KB, 314 views)
Jan 26 '11 #1
0 1048

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: BT3 | last post by:
I am trying to replace a single record in a flat file. The file is relatively small and no need for database. I open the file, and save the ftell() value in a variable. I read a record using...
0
by: mwazir | last post by:
Dear all, I have a requirement while parsing a delimited flat file and I was wondering if there is a simpler solution that what I have in mind. Basically I need to pull out a line from the flat...
2
by: platinumbay | last post by:
If I have a flat file, and an XSD, is it possible to convert the delimited flat file to XML? Thanks, Stephen Andrews
2
by: Neural | last post by:
Hi, I was wondering if anybody knew of any other ways of efficiently parsing a flat file into SQL Server 2000 using C#. The flat files are tab delimited. And the general file size is around 1 GB...
1
by: elena | last post by:
I have large flat file with fixed record lenght, i have to parse record and populate table with multi columns with the data, please any links or code snipets to this topic will be greatly...
13
by: JJ | last post by:
I have a need to input a large tab delimited text file, which I will parse to check it has the expected columns, before allowing the user to submit it to the database. The user may paste the file...
3
by: PaulH13 | last post by:
Hi, I have a pipe delimited flat file database of students that I mail out certificates to. I wrote a label preparation script which basically passes through the file and prepares a mailing...
3
by: stephen | last post by:
I had to use use ssis 2005 in a short project recently & had little time to work it out. I was importing a whole bunch of flat files into SQL Server tables with many derived columns and...
3
by: Flugeldorph | last post by:
I am new to Visual Basic, my preferred languages is C or C++. I am trying to load a local database with data that I am reading from a delimited flat file. I have created the database tables and...
7
by: crochunter | last post by:
Hi I was trying to count rows and columns in a tab delimited flat file. Like here in example below i should be able to detect automatically the no of rows and columns. So I should get 5 columns...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.