Collecting Data in an Output File

2004-08-31 Thread Errin Larsen
Hi Perl Gurus!

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.

I have successfully coded some Perl that pulls that data from the
system, but now I need to be able to a) determine if the file is empty
and, if so, start the file by adding the CPUx output. b) if the file
is not empty, add the next set of readings to the end of each line.

for a), I know I can do:

  if( -e FILE)

that's no problem.  My problem is b).  How can I read in all the lines
into their on arrays and add some data to the end?

my @wholefile = FILE;

Puts all the lines, one line per array element (with \n at the end).
 So, I can easily iterate through and chomp the \n out, then I can
add the latest temperature reading onto the end.

Ok, maybe I answered my own question.

So, let me take it a step further.  What if I wanted to read a
particular time's data out of the file; or add one into the middle? 
Can I get an array of arrays, Where one Array holds other arrays, each
other array holding the data (split into elements) of one line?

--Thanks

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




Re: Collecting Data in an Output File

2004-08-31 Thread Chris Devers
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



Re: Collecting Data in an Output File

2004-08-31 Thread Errin Larsen
On Tue, 31 Aug 2004 14:07:14 -0400 (EDT), Chris Devers
[EMAIL PROTECTED] wrote:
 On Tue, 31 Aug 2004, Errin Larsen wrote:
 
  I am collecting temperature data from the CPUs in my system.  

SNIP

 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.

The above is good stuff.  I'll work on this.  There are some problems,
but I'll get to them below.

 
  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...

SNIP

Thanks Chris.  This is the sort of thing I was looking for!  Now I'll
go play with your suggestions some.  The problem I'm gonna have is
that the file format I suggested is really what I need.  HOWEVER, I
don't need that format when I'm collecting the data, just when I get
ready to use that data.  So I think I'll try collecting it as you
suggest in the 2nd suggestion above, and then work on a script that
will convert THAT file into the format I need.  Thanks again!

--Errin

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