FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Perform maths based on a number in a text file
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Perform maths based on a number in a text file [message #183805 is a reply to message #183800] Wed, 20 November 2013 13:03 Go to previous messageGo to previous message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma:
Senior Member
On 20/11/13 11:50, drkirkby(at)gmail(dot)com wrote:
> On Monday, 18 November 2013 13:19:50 UTC, The Natural Philosopher wrote:
>
>> I would strongly recommend using a database: setting that up is a bit of
>>
>> a faff, but once set up it provides the sort of 'update by one screen,
>>
>> read buy many others' sort of access that you need. And wont need any
>>
>> file level parsing - you will get your numbers as an array from a Mysql
>>
>> type query.
>>
>>
>>
>> You can even embed the mathematics in the query instead of doing it on
>>
>> PHP, if you want.
>>
>>
>>
>> But all this assumes you are willing to knuckle down and learn basic
>>
>> SQL, mysql administration, and how to set up web forms and the like.
>>
>>
>>
>> If you are as you sound, a relative newcomer, this is a few weeks work :-(
>
> Hi,
> thank you for your thoughs. To be honest, I'm not over keen on using a database. I'm not a web designer, but an engineer trying to get data about his product on a web page. Maybe the constants will be updated in the future, but perhaps they never will. It is certainly not something I expect to do on a regular basis. The idea of spending 3 days working on this does not attract me.
>
> To answer the question asked by someone else about how the constants will get updated. It will be me doing it manually - no program will update them. Maybe file_get_contents will do what I want.
>

It will, but instead of learning the database thingy, you need to learn
the file parse thingy...


Now it so happens I do both, so this is a code fragment that loads a
'one value per line' file of floating point numbers into PHP variables..

Its from this web page

http://www.gridwatch.templar.co.uk

and the results drive the dials there.

The file is itself generated by an asychronous process that scrapes data
from elsewhere.

The data comes in as megawatts: I wanted floating point gigawatts hence
the conversions.

------------------------
$retval=file_get_contents("/tmp/nationalgrid");
$results=explode("\n",$retval);
$demand=$results[12];
$freq=$results[13];
$nuclear=sprintf("%0.2f",$results[4]/1000);
$coal=sprintf("%0.2f",$results[3]/1000);
$ccgt=sprintf("%0.2f",$results[0]/1000);
$wind=sprintf("%0.2f",$results[5]/1000);
$hydro=sprintf("%0.2f",$results[7]/1000);
$pumped=sprintf("%0.2f",$results[6]/1000);
$french=sprintf("%0.2f",$results[9]/1000);
$irish=sprintf("%0.2f",$results[10]/1000);
$dutch=sprintf("%0.2f",$results[11]/1000);
$ew=sprintf("%0.2f",$results[14]/1000);
$oil=sprintf("%0.2f",$results[2]/1000);
$ocgt=sprintf("%0.2f",$results[1]/1000);
$other=sprintf("%0.2f",$results[8]/1000);

---------------------------------------

If you can organise your file like that then this is about as crude and
simple as it gets.

This is a snapshot of the file

$cat /tmp/nationalgrid
13978
0
0
17061
7533
5303
281
451
614
48
-252
598
45944
50.133
0.000
-----------------------------------------


Note that PHP will dependent on context look at a string like "3.72" and
interpret it as a string OR a floating point number, depending on the
operation you are doing on it. Dividing or multiplying such a string
forces it to be treated like a number. Printing it produces a string etc.

printf and sprintf are handy ways to format the number for display purposes.

explode is a handy way to split a string into an array of chunks
delimited by another string - in this case the end of line charracter.

ensure that the file you create is readable by the web server you are using.


> Dave
>


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: array search part 2
Next Topic: What do you think about this book on API ?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 04:52:56 GMT 2024

Total time taken to generate the page: 0.28522 seconds