|
Posted by petersprc on 09/19/06 17:58
Hi Patrick,
Using php's associative arrays, you can map a key, which in your case
is the result value you calculated, to a value, which would be the file
contents. For example:
$results[strvalue($yearDayValue)] = file_get_contents('file.dat');
The strvalue function is used to convert the float into a string,
because floats are truncated to integers when used directly as keys in
an associative array.
Here's a more extended usage example:
<?
function getResults()
{
$results = array(); // Results array
// Get all *.data files in the current dir
$files = glob('*.data');
foreach ($files as $file) {
// Read the contents of the file
$contents = file_get_contents($file);
// Call your function to calculate the result
$result = getMyResult($contents);
// Store the result. Convert the floating point key
// into a string because float keys are truncated to
// integers when used directly.
$results[strval($result)] = $contents;
}
// Now $results maps the individual result to each
// file's contents
print_r($results);
echo "\n";
echo "There are " . count($results) . " results.\n\n";
// Iterate over the results
foreach ($results as $key => $value) {
echo "The year-day value is $key and the file contains:\n\n" .
"$value\n";
}
}
function getMyResult($contents)
{
$result = 0;
$lines = explode("\n", $contents);
foreach ($lines as $line) {
if ($line != '') {
list($descent, $a, $b, $date, $time, $c, $d, $e, $f) =
explode(' ', $line);
$result += $f;
}
}
return $result;
}
getResults();
?>
Best Regards,
John Peters
Patrick wrote:
> Hi All,
>
> I have numerous files that look like the chunk below. From the date and
> time I calculate a year day value. For the example that value is
> 148.67721064815
>
> I'm not an expert yet at dealing with arrays, but I get around well in
> php otherwise (reading data files, loops, functions, etc).
>
> My question is how would I go about creating an array that would hold
> the year day as a key and the entire file contents in the next element?
> Then when I advance to the next file do the same thing so that my ending
> array would come out something like this;
>
> [yearday file1][file1 contents]
> [yearday file2][file2 contents]
> [yearday file3][file3 contents]
> ...
> ..
> .
>
> Or is what I am wanting to do out of the question?
>
> Appreciate your help.
>
> Patrick
>
> File example;
>
> Descent 27.4279 -83.0965 05-29-06 16:15:11 55.052 26.52 0.95 35.28
> Descent 27.4279 -83.0965 05-29-06 16:15:11 55.014 26.51 1.95 35.26
> Descent 27.4279 -83.0965 05-29-06 16:15:11 55.047 26.50 3.04 35.29
> Descent 27.4279 -83.0965 05-29-06 16:15:11 54.957 26.47 4.29 35.24
> Descent 27.4279 -83.0965 05-29-06 16:15:11 54.947 26.45 5.33 35.25
> Descent 27.4279 -83.0965 05-29-06 16:15:11 54.871 26.41 6.42 35.22
> Descent 27.4279 -83.0965 05-29-06 16:15:11 54.875 26.36 7.52 35.25
> Descent 27.4279 -83.0965 05-29-06 16:15:11 54.884 26.30 8.65 35.31
> Descent 27.4279 -83.0965 05-29-06 16:15:11 54.887 26.24 9.78 35.35
>
>
> --
> Patrick A. Smith Assistant System Administrator
> Ocean Circulation Group - USF - College of Marine Science
> http://ocgweb.marine.usf.edu Phone: 727 553-3334
>
> The trouble with doing something right the first time is that nobody
> appreciates how difficult it was. - La Rochefoucauld
Navigation:
[Reply to this message]
|