|
Posted by FB on January 26, 2005, 7:43 pm
Please log in for more thread options
Hi,
first of all, I must be a fool, thinking I can write monitoring software
using RRDs, Mysql and GD while being a very very newbie in perl..
My problem is, I derive data out of RRD, with theses datapoints I try to
graph using GD.
After running the script it says:
Argument "0.0375,0.0363,0.0282,0.0353,0.0396,0.0370,0.0235,0.0228,..." isn't
numeric in numeric eq (==) at
/usr/lib/perl5/site_perl/5.8.3/GD/Graph/axestype.pm line 1452
The lines in the script are: (I know the sed lines look a 'bit' newbie-like
:-))
==============//=================
push @RRDATA, `$rrdtool fetch $rrddir/$DatabaseName AVERAGE |/bin/grep -v
ds |/bin/grep -v nan |/usr/bin/tail -12`;
foreach $_ (@RRDATA) {
($time, $unerr, $corr, $uncorr, $uncorrpercent, $corrpercent) = split('
');
$upct = `printf "%6.4f" $uncorrpercent`;
$timerecord = join(",",$timerecord,""$time"");
$timerecord =~ s/^,//g;
$timerecord =~ s/://g;
$timerecord = join("$timerecord",""$time"");
$datarecord = join(",",$datarecord, $upct);
$datarecord =~ s/^,//g;
}
my @data = ([$timerecord],[$datarecord]);
my $mygraph = GD::Graph::bars->new(500, 300);
$mygraph->set(
x_label => 'Time',
y_label => 'Cer %',
title => $DatabaseName,
) or warn $mygraph->error;
my $myimage = $mygraph->plot(@data) or die $mygraph->error;
print "Content-type: image/pngnn";
print $myimage->png;
===============//======================
anybody a clue??
Thanks a lot in advance!
Frans
|
|
Posted by Brian McCauley on January 26, 2005, 8:45 pm
Please log in for more thread options
FB wrote:
> first of all, I must be a fool, thinking I can write monitoring software
> using RRDs, Mysql and GD while being a very very newbie in perl..
> My problem is, I derive data out of RRD, with theses datapoints I try to
> graph using GD.
> After running the script it says:
> Argument "0.0375,0.0363,0.0282,0.0353,0.0396,0.0370,0.0235,0.0228,..." isn't
> numeric in numeric eq (==) at
> /usr/lib/perl5/site_perl/5.8.3/GD/Graph/axestype.pm line 1452
Looks like you are passing a comma delimited list to something that
doesn't expect it.
>
> The lines in the script are: (I know the sed lines look a 'bit' newbie-like
> :-))
>
> ==============//=================
> push @RRDATA, `$rrdtool fetch $rrddir/$DatabaseName AVERAGE |/bin/grep -v
> ds |/bin/grep -v nan |/usr/bin/tail -12`;
> foreach $_ (@RRDATA) {
> ($time, $unerr, $corr, $uncorr, $uncorrpercent, $corrpercent) = split('
> ');
> $upct = `printf "%6.4f" $uncorrpercent`;
> $timerecord = join(",",$timerecord,""$time"");
> $timerecord =~ s/^,//g;
> $timerecord =~ s/://g;
> $timerecord = join("$timerecord",""$time"");
> $datarecord = join(",",$datarecord, $upct);
> $datarecord =~ s/^,//g;
> }
That code has so many things wrong with it that it is almost impossible
to know where to start. I really mean that.
I know let's start with the easy stuff.
> foreach $_ (@RRDATA) {
If you are goinf to tell foreach to use some variable other the default
then use some variable other than the default! If you want to use the
default don't mention it.
foreach (@RRDATA) {
> ($time, $unerr, $corr, $uncorr, $uncorrpercent, $corrpercent) =
split('
> ');
Are you really splitting on newline? Or are you splitting on space?
Would it not be simpler to split on the default?
Always declare all variables in the smallest applicable scope unless you
have a positive reason to do otherwise. Start doing this from the
outset. The longer you leave it the more of your own time and that of
other people you will waste and the harder it will be to retro-fit the
correct behaviour.
There's no reason to store that which you don't want.
my ($time, undef, undef, undef, $uncorrpercent)=split;
> $upct = `printf "%6.4f" $uncorrpercent`;
You forgot to chomp(). But Perl has a builting sprintf() function anyhow.
my $upct = sprintf '%6.4f', $uncorrpercent;
> $timerecord = join(",",$timerecord,""$time"");
That is more simply written
$timerecord .= ","$time"";
But you probably don't want a comma delimited string you probably want
an array.
push @timerecord, $time;
> $timerecord =~ s/^,//g;
What do you think /g does? How many beginnings do you think a string
has? Why is this inside the loop anyhow?
> $timerecord =~ s/://g;
Why is this inside the loop? It would be more natural to use tr/://d
> $timerecord = join("$timerecord",""$time"");
This is more simply written as:
$timerecord = ""$time"";
So all that work you've just done to construct something in $timerecord
was wasted.
See also FAQ: What's wrong with always quoting "$vars"?
> $datarecord = join(",",$datarecord, $upct);
That is more simply written
$datarecord .= ",$upct";
But you probably don't want a comma delimited string you probably want
an array.
push @datarecord, $upct;
> $datarecord =~ s/^,//g;
What do you think /g does? How many beginnings do you think a string
has? Why is this inside the loop anyhow?
> my @data = ([$timerecord],[$datarecord]);
I suspect that you didn't want an array containing references to two
arrays each containing a single string. I suspect you meant:
my @data = (@timerecord,@datarecord);
I suggest you take a look at the awfull mess you've created in @data with.
use Data::Dumper;
print Dumper @data;
OK that was a fisrt-pass looking at what was wrong with each line
individually. Now lets start aftresh.
Please remember the rule "always write less unless this would be less
readable".
my(@timerecord,@datarecord);
foreach (@RRDATA) {
my ($time, undef, undef, undef, $uncorrpercent ) = split;
push @datarecord, sprintf '%6.4f', $uncorrpercent;
push @timerecord, $time;
}
my @data = (@timerecord,@datarecord);
Note you can actually combine the creation of @data with the declaration
of the inner arrays.
my @data = my(@timerecord,@datarecord);
But that's probably not as clear and uses a what I once proposed as the
most obscure/least intuative feature of Perl.
http://groups-beta.google.com/group/comp.lang.perl.misc/msg/3be4ad8ff3bc836e
|
|
Posted by Brian McCauley on January 26, 2005, 8:49 pm
Please log in for more thread options
Brian McCauley wrote:
> foreach (@RRDATA) {
> my ($time, undef, undef, undef, $uncorrpercent ) = split;
> push @datarecord, sprintf '%6.4f', $uncorrpercent;
> push @timerecord, $time;
> }
Oops, forgot you wanted to remove the ':' from $time;
foreach (@RRDATA) {
my ($time, undef, undef, undef, $uncorrpercent ) = split;
push @datarecord, sprintf '%6.4f', $uncorrpercent;
$time =~ tr/://d;
push @timerecord, $time;
}
|
|
Posted by Brian McCauley on January 27, 2005, 1:55 pm
Please log in for more thread options Brian McCauley wrote:
>
> FB wrote:
>
>> $upct = `printf "%6.4f" $uncorrpercent`;
>
> You forgot to chomp().
Er, bu*****t!
|
|
Posted by FB on January 27, 2005, 4:34 pm
Please log in for more thread options Brian McCauley wrote:
> Always declare all variables in the smallest applicable scope unless you
> have a positive reason to do otherwise. Start doing this from the
> outset. The longer you leave it the more of your own time and that of
> other people you will waste and the harder it will be to retro-fit the
> correct behaviour.
>
> There's no reason to store that which you don't want.
>
> my ($time, undef, undef, undef, $uncorrpercent)=split;
>
yep, I got your point, always use my $var, I'll read the faq carefully,
promise! I didn't know the undef phrase..
>
>> my @data = ([$timerecord],[$datarecord]);
>
> I suspect that you didn't want an array containing references to two
> arrays each containing a single string. I suspect you meant:
I tried to emulate a comma-seperated string like the GD example script.
>
> my @data = (@timerecord,@datarecord);
>
> I suggest you take a look at the awfull mess you've created in @data with.
>
> use Data::Dumper;
> print Dumper @data;
Yep, I did and indeed, what a mess.. :-(
>
> OK that was a fisrt-pass looking at what was wrong with each line
> individually. Now lets start aftresh.
>
> Please remember the rule "always write less unless this would be less
> readable".
>
> my(@timerecord,@datarecord);
>
> foreach (@RRDATA) {
> my ($time, undef, undef, undef, $uncorrpercent ) = split;
> push @datarecord, sprintf '%6.4f', $uncorrpercent;
> push @timerecord, $time;
> }
>
> my @data = (@timerecord,@datarecord);
>
> Note you can actually combine the creation of @data with the declaration
> of the inner arrays.
>
> my @data = my(@timerecord,@datarecord);
>
> But that's probably not as clear and uses a what I once proposed as the
> most obscure/least intuative feature of Perl.
>
>
Brian,
many many thanks, it worked instantly, I checked @data afterwards with the
dumper and I see some differences..
Lucky for me this was only my third short perl script, next time I'll try to
read a bit more before using your precious time!
Thanks again!
Frans
|
| Similar Threads | Posted | | GD::Graph - how to get rid of leading gap in line graph? | October 1, 2004, 10:56 am |
| Graph.0.69 Module---how to get a DAG from graph with cycles | January 23, 2006, 2:50 pm |
| Image magic problem Use of uninitialized value in numeric gt (>) | March 20, 2006, 7:12 pm |
| GD::GRAPH two_axes | December 8, 2004, 6:01 pm |
| GD graph, Several Colors per Bar? | May 30, 2005, 6:52 pm |
| Name for a new Tk Graph module | April 4, 2007, 6:17 am |
| Perl GD::Graph module: bug? | August 3, 2004, 8:42 pm |
| GD::Graph => How to set maximum value manually on Y ? | October 6, 2004, 6:35 pm |
| Do we need Graph::Clique module? | November 10, 2004, 2:58 pm |
| ANNOUNCE: Graph-ReadWrite 2.00 | January 2, 2005, 9:17 pm |
|