By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,498 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,498 IT Pros & Developers. It's quick & easy.

Parsing a txt file

P: n/a
I would need a "script" that parses the following information in
'outdoor.txt'.
What i actually need are those variables (for example $yTemp). I'd do it
myself but my knowhow lacks ;D
outdoor.txt is input and variables are "output" which i need to make graphs
(jgraph)

-------OUTDOOR.txt-------
Date; Time; Temp ÝC
21.11.2003; 18:21:39; -2.69
24.12.2003; 23:10:45; 0.06
01.01.2004; 00:00:29; -5.75
01.01.2004; 00:43:37; -6.13
03.01.2004; 22:13:38; -8.44
05.01.2004; 07:38:58; -4.06
05.01.2004; 09:33:19; -4.50
-------OUTDOOR.txt-------

i know how to read the last line and get three variables:
....
$in_part = explode(";", $lastRow);
$in_pvmOUT = $in_part[0]; // Date = 05.01.2004
$in_aikaOUT = $in_part[1]; // Aika = 09:33:19
$in_lampoOUT = $in_part[2]; // Temp ÝC = -4.50

But i don't know how to get more values at once and feed it to the array:

Values should be in the following format:
$yTemp = array(-2.69, 0.06, -5.78); // Average temperatures for each month
$xAverage = array("Nov 2003","Dec 2003","Jan 2004");

It actually has values for every minute and every day.
So how should it be done if i want to draw a graph for "today", say
5.1.2004.
$yTempToday =
array(-4.38, -4.44, -4.44, -4.44, -4.44, -4.44, -4.44, -4.50, -4.50, -4.50,
-4.50, -4.44, -4.44, -4.50);
$xTimeDecimal = array(9.40, 9.42, 9.43, 9.45, 9.47, 9.48, 9.50, 9.52, 9.54,
9.56, 9.58, 9.59, 9.61, 9.63);
or $xTimeActual = array("09:23:37", "09:24:42", "09:25:46", "09:26:51",
"09:27:56", "09:29:00", "09:30:05", "09:31:10", "09:32:15", "09:33:19",
"09:34:24", "09:35:29", "09:36:34", "09:37:39");
-------OUTDOOR.txt-------
Date; Time; Temp ÝC
21.11.2003; 18:21:39; -2.69
24.12.2003; 23:10:45; 0.06
01.01.2004; 00:00:29; -5.75
01.01.2004; 00:43:37; -6.13
03.01.2004; 22:13:38; -8.44
05.01.2004; 09:23:37; -4.38
05.01.2004; 09:24:42; -4.44
05.01.2004; 09:25:46; -4.44
05.01.2004; 09:26:51; -4.44
05.01.2004; 09:27:56; -4.44
05.01.2004; 09:29:00; -4.44
05.01.2004; 09:30:05; -4.44
05.01.2004; 09:31:10; -4.50
05.01.2004; 09:32:15; -4.50
05.01.2004; 09:33:19; -4.50
05.01.2004; 09:34:24; -4.50
05.01.2004; 09:35:29; -4.44
05.01.2004; 09:36:34; -4.44
05.01.2004; 09:37:39; -4.50
-------OUTDOOR.txt-------
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jari wrote:
I would need a "script" that parses the following information in
'outdoor.txt'.
...
-------OUTDOOR.txt-------
Date; Time; Temp ÝC
21.11.2003; 18:21:39; -2.69
24.12.2003; 23:10:45; 0.06
01.01.2004; 00:00:29; -5.75
01.01.2004; 00:43:37; -6.13
03.01.2004; 22:13:38; -8.44
05.01.2004; 07:38:58; -4.06
05.01.2004; 09:33:19; -4.50
-------OUTDOOR.txt-------
...


You can use the CSV function:
http://www.php.net/manual/en/function.fgetcsv.php
Jul 17 '05 #2

P: n/a
"Jari" <no*@here.fi> schreef in bericht news:bt**********@news.cc.tut.fi...
I would need a "script" that parses the following information in
'outdoor.txt'.
-------OUTDOOR.txt-------
Date; Time; Temp ÝC
21.11.2003; 18:21:39; -2.69
24.12.2003; 23:10:45; 0.06
01.01.2004; 00:00:29; -5.75
01.01.2004; 00:43:37; -6.13
03.01.2004; 22:13:38; -8.44
05.01.2004; 07:38:58; -4.06
05.01.2004; 09:33:19; -4.50
-------OUTDOOR.txt-------
Values should be in the following format:
$yTemp = array(-2.69, 0.06, -5.78); // Average temperatures for each month
$xAverage = array("Nov 2003","Dec 2003","Jan 2004");
So how should it be done if i want to draw a graph for "today", say
5.1.2004.
$yTempToday =
array(-4.38, -4.44, -4.44, -4.44, -4.44, -4.44, -4.44, -4.50, -4.50, -4.50, -4.50, -4.44, -4.44, -4.50);
$xTimeDecimal = array(9.40, 9.42, 9.43, 9.45, 9.47, 9.48, 9.50, 9.52, 9.54, 9.56, 9.58, 9.59, 9.61, 9.63);
or $xTimeActual = array("09:23:37", "09:24:42", "09:25:46", "09:26:51",
"09:27:56", "09:29:00", "09:30:05", "09:31:10", "09:32:15", "09:33:19",
"09:34:24", "09:35:29", "09:36:34", "09:37:39");


Try the code below. If you have difficulties understanding what the code
does, look up what the functions array(), file(), count(), number_format(),
date(), trim(), array_sum(), strtotime() and empty() do in the PHP manual at
www.php.net or post any questions you have. I had wanted to put comments
everywhere but wasn't sure that you would want that. I assumed you know how
global variables are changed in functions, if not, just ask me how to use
this code.
Note that this code doesn't check the input very much, for example it simply
assumes that it should skip the first line of OUTDOOR.txt. The safest code
is of course longer, or it would use more advanced input processing
techniques like regular expressions. But I figured the code below would
provide a working and understandable solution.

Remon Huijts
<?php
function per_month() {
global $yTemp, $xLabel;
$yTemp = array();
$xLabel = array();
$lines = file('OUTDOOR.txt');
$temps = array();
$year = 0;
$month = 0;

for ($i = 1; $i < count($lines); $i++) {
$this_line = explode(';', trim($lines[$i]));
$date = explode('.', trim($this_line[0]));
$time = explode(':', trim($this_line[1]));

if ($date[2] > $year || $date[1] > $month) {
if (count($temps) > 0) {
$yTemp[] = number_format(array_sum($temps) / count($temps),
2);
$xLabel[] = date('M Y', strtotime("$year-$month-01"));
}
$temps = array();
$month = $date[1];
$year = $date[2];
}
$temps[] = trim($this_line[2]);
}
if (count($temps) > 0) {
$yTemp[] = number_format(array_sum($temps) / count($temps), 2);
$xLabel[] = date('M Y', strtotime("$year-$month-01"));
}
}

function per_day($day = '') {
global $yTemp, $xLabel, $xValue;
// $day is like '05.01.2004' WITH leading zero, or empty for today
if (empty($day)) $day = date('d.m.Y');
$yTemp = array();
$xLabel = array();
$xValue = array();
$lines = file('OUTDOOR.txt');

for ($i = 1; $i < count($lines); $i++) {
$this_line = explode(';', trim($lines[$i]));
if (trim($this_line[0]) == $day) {
$time = explode(':', trim($this_line[1]));
$minutes = (float)$time[1] + (float)$time[2] / 60;

$yTemp[] = trim($this_line[2]);
$xLabel[] = trim($this_line[1]);
$xValue[] = (integer)$time[0] + '.' + number_format(($minutes /
60), 2);
}
}
}
?>
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.