Jump to content

multi file / multi tasked php


trauch

Recommended Posts

So as to not confuse anyone regarding my progress I have started a new thread.

 

I'm working on a script that will perform a few tasks

1) Load multiple text files each of which contains a line of numbers

2) In each text file remove white space and then add all lines

3) Append the contents of all of these text files to another text file

4) Add all numbers in the resulting text file

5) Save that resulting number as a new text file whose name is generated as the current date. (ie 2008-12-22.txt)

 

I've got some messy code below but would appreciate any assistance.

 

<?php

$data1 = file('textfile1.txt');
$data1 = str_replace(' ','',$data1);
echo 'sum($data1) = ' . array_sum($data1) . "\n";
$sum = array_sum($data1) . "\n";

$data2 = file('textfile2.txt');
$data2 = str_replace(' ','',$data2);
echo 'sum($data2) = ' . array_sum($data2) . "\n";
$sum2 = array_sum($data2) . "\n";

$data3 = file('textfile3.txt');
$data3 = str_replace(' ','',$data3);
echo 'sum($data3) = ' . array_sum($data3) . "\n";
$sum3 = array_sum($data3) . "\n";


$fp = fopen('textmaster.txt', 'a');
fwrite($fp, $sum);
fwrite($fp, $sum2);
fwrite($fp, $sum3);

fclose($fp);

?>

Link to comment
Share on other sites

There are a few ways of doing it, but this is how I would do it:

<?php
// Make an array of the filenames to sum
$fileNames = array('textfile1.txt', 'textfile2.txt', 'textfile3.txt');

// Open the file and zero out the net sum
$fp = fopen('textmaster.txt', 'a');
$netSum = 0;

// Add the numbers and append to the file
foreach($fileNames as &$fileName) {
$content = str_replace(' ', '', file($fileName)); // Perhaps use preg_replace to remove ALL whitespace instead of just spaces?
$sum = array_sum($content);
echo 'Sum of "' . $fileName . ': ' . $sum;
$netSum += $sum;
fwrite($fp, $sum);
}

// Clean up
fclose($fp);

// Create the day file
file_put_contents(date('Y-n-j') . '.txt', $netSum);
?>

 

I didn't test it, but you get the gist of it.

Link to comment
Share on other sites

With the help of genericnumber1 I'm almost there but I would appreciate any input.  Since Im using php4 I cant use file_put_contents and am still trying to figure out how to save the file as the current date (2008-12-22.txt). I know I have to use fwrite but am not sure how to do this.

 

Here's what I have thus far.

 

<?php
// Make an array of the filenames to sum
$fileNames = array('textfile.txt', 'textfile2.txt', 'textfile3.txt');

// Open the file and zero out the net sum
$fp = fopen('textmaster.txt', 'a');
$netSum = 0;

// Add the numbers and append to the file
foreach($fileNames as $fileName) {
   $content = str_replace(' ', '', file($fileName)); 
   $sum = array_sum($content);
   echo 'Sum of "' . $fileName . ': ' . $sum;
   $netSum += $sum;
   fwrite($fp, $sum);
}

// Clean up
fclose($fp);

// Create the day file
fwrite(date('Y-n-j') . '.txt', $netSum);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.