> -----Original Message----- > From: Christopher J. Crane [mailto:[EMAIL PROTECTED]] > Sent: 06 January 2003 17:12 > > Ok here is what I did but it does not do anything. > I verified that is opening the file ok and everything, but it > shows nothing. > It doesn't even produce an error. I am sure there is an > easier way than > looping twice, but this is how I have it for now.
Hoo, boy! there's so many things wrong with this I hardly know where to start! However... > $Lines = array(); > $TempDir = "tempdata"; > $DataFromFile = file("$TempDir/$Dat.txt"); I hope you've assigned $Dat a value elsewhere -- it's not in the fragment posted here! > while(list(,$oneline) = each($DataFromFile)) { > array_push($Lines, $oneline); > } Why on earth do you do this? It's converting the array of lines in $DataFromFile into -- er -- an identical array of lines in $Lines -- and *extremely* inefficiently, too! Just do: $Lines = file("$TempDir/$Dat.txt"); Except, hang on, the following code then writes a whole new set of values in to $Lines, so we can just forget the above while loop totally! > $LineCount = 1; > while(list(,$oneline) = each($DataFromFile)) { Use foreach in preference to a while(each()) loop, so: foreach ($DataFromFile as $oneline) { > $PriorLineCount = $LineCount - 1; Drop this -- it's not necessary. > list($OctetsInB,$OctetsOutB,$TimeB) = split("|", > $Lines[$LineCount]); > list($OctetsInA,$OctetsOutA,$TimeA) = split("|", > $Lines[$PriorLineCount]); (1) H'mmm -- you've gone to the trouble of putting the current line in $oneline -- so why ignore it and dig $Lines[$LineCount] out of the array all over again? (2) You don't need to split both lines here -- this way you'll be doubling the work by splitting each line twice. (3) explode() is the preferred name for split(). (4) This split() is good to get the three parts of the line delimited by |, but none of these is individually a number: > > > > Here is an example of the file the data is written to: > > > > OctetsIn:4300492881|OctetsOut:4300544503|UnixTime:1041629017 > > > > OctetsIn:4305184236|OctetsOut:4305234971|UnixTime:1041629031 You'll have (e.g.) $OctetsIn=='OctetsIn:4300492881', $OctetsOut=='OctetsOut:4300544503', which will each evaluate to zero -- not good! You need to further split each one on the internal ":". I'd do the whole trick like this: $values = explode('|', $oneline); foreach ($values as $k=>$text) { list(,$values[$k]) = explode(':', $values[$k]); } You'll see I've exploded the line into an arrray, and then exploded each element of the array back into itself; this gives us a 3-element array with the required values in it. We'll find out why I've done it this way in a minute! OK, so far we've done everything to every line -- but when it gets to calculating the answers, we don't want to do this for the first line (as there's no previous one!), so: if ($LineCount>1) { > // After much help and work with Harry, this is the formula > we came up > with to show data rates over time. > // (((Counter_Now - Counter_Before) / (Time_Now - > Time_Before(converted to > seconds))) * 8)) / 1000 = kbits per hour > $kbitsout = ((($OctetsOutB - $OctetsOutA) / ($TimeB - > $TimeA)) * 8 ) / > 1000; This bit I believe -- except that it's kbits/sec, not per hour! -- but of course it needs adjusting for the fact that the values are now in an array; also, so far we don't know where the values for the previous line are, so I'll come back to this! > print "$kbitsout Kbits - $LineCount<br>\n"; } // end of the if($LineCount>1) Now we need to remember the values we extracted for this line, so that we can use them as the *previous* values when we process the next line on the next iteration of the foreach loop; as I've put the values in an array this is easy: $prev_values = $values; (If I'd produced three separate variables, I'd have to do three assignments!) > $LineCount++; > } OK, so far so good -- but what about that middle bit that I left out? Well, we now know that we will have the current values in $values[], and the previous values in $prev_values[], so the calculation becomes: $kbitsout = ($values[1]-$prev_values[1]) / ($values[3]-$prev_values[3]) / 125; And that's it, eventually. Just to put it all back together, this is how it's ended up: $TempDir = "tempdata"; $DataFromFile = file("$TempDir/$Dat.txt"); $LineCount = 1; foreach ($DataFromFile as $oneline) { $values = explode('|', $oneline); foreach ($values as $k=>$text) { list(,$values[$k]) = explode(':', $values[$k]); } if ($LineCount>1) { $kbitsout = ($values[1]-$prev_values[1]) / ($values[3]-$prev_values[3]) / 125; print "$kbitsout Kbits - $LineCount<br>\n"; } $prev_values = $values; $LineCount++; } Final caveat emptor: I haven't actually tested any of this, but it shouldn't be far wrong! Hope that lot proves helpful, and good luck with it! Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php