On Tue, 31 Aug 2004, Errin Larsen wrote:

I am collecting temperature data from the CPUs in my system.  I want
to write this data into a comma-seperated text file, like so:

CPU1,65,63,62,64
CPU2,65,64,64,62
CPU3,64,65,66,64

Each line represents one CPU's temperature readings. Each column represents the time the reading was taken. So, if I was taking the readings once an hour, the above sample would be, for example, 12 Noon, 1pm, 2pm and 3pm.

This is nitpicking, but have you considered inverting that? A format like this might be easier to work with:


    time,cpu1,cpu2,cpu3
    Tue Aug 31 12:00:00 EDT 2004,65,65,64
    Tue Aug 31 13:00:00 EDT 2004,63,64,65
    Tue Aug 31 14:00:00 EDT 2004,62,64,66
    Tue Aug 31 15:00:00 EDT 2004,64,62,64

This way, you can simply append to the file with the timestamp and the readings from each of the CPUs you're monitoring, rather than having to open the file and append to each line.

The downside is that if you add CPUs, the file falls apart because you need to add / change / remove a column. The fix for this is to just have one CPU per line, and identify it:

    time,cpu,temp
    Tue Aug 31 12:00:00 EDT 2004,cpu1,65
    Tue Aug 31 12:00:00 EDT 2004,cpu2,65
    Tue Aug 31 12:00:00 EDT 2004,cpu3,64
    ...

This is more verbose, obviously, but also more flexible.

How can I read in all the lines into their on arrays and add some data to the end?

For the one I've got above, it would be something like...

    while ( <> ) {
        my ($ts, $cpu, $temp) = split("," $_);
        push (@{ $processors{$cpu} }, $temp);
    }

Then to get at the data, do something like

    foreach $cpu ( sort keys %processors ) {
        print "$cpu: @{ $processors{ $cpu }}\n"
    }

...or something like that...



--
Chris Devers      [EMAIL PROTECTED]
http://devers.homeip.net:8080/blog/

np: 'The Muppet Show Theme'
     by The Muppets
     from 'The Muppet Show'

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to