472,993 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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, 310 views)
Jan 26 '11 #1
0 1037

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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.