Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble graphing text file

Status
Not open for further replies.

ITadvice

IS-IT--Management
Jul 22, 2008
38
US
I am trying to graph a temp file. I am using GD::Graph::Data to create the graph. Here is some of my code.

Code:
        open(FILE, "<timedata.txt") or return;  # Open file
        my $fh = IO::File->new_tmpfile or die "Unable to make new temp file: $!"; 
        $fh->autoflush(1);                      
        my $headercount = 1;                            
        my $discard = <FILE> for 1 .. $headercount;     
        foreach $line (<FILE>) {
                chomp $line;;            
                ($timestamp, undef, undef, $out, undef, undef, undef, $in, undef)=split(/\t/, $line);   
                my $datetime = DateTime::Format::Excel->parse_datetime($timestamp);
                $timestamp = $datetime->ymd . $datetime->hms;
                print $fh "$timestamp $out $in\t\n";    
                seek ($fh, 0, 0) or die "Seek: $!";
        }
       close (FILE);

#       Create graph of the file     
        my $datafile = GD::Graph::Data->new();                     
        $datafile->read(file => ‘$fh’, delimiter => '\t');        
        $datafile = $datafile -> copy(wanted => [0, 1, 2]);             
        while (my @row = $fh->fetchrow_array())        <-------- This is where I have trouble
        {
                $datafile ->add_point($row);
        }
        my $graph = GD::Graph::points->new(400, 300);                  
        $graph->set(

        x_label     => 'Time/Date',                             

        y_label     => 'Count',                         

        title       => "Count of Data", 

        ) or warn $graph->error;

        my $image = $graph->plot($data) or die $graph->error;  

        open(GRAPH, ">graph.png") or die "Not able to open image file\n";         
        binmode GRAPH;                                          
        print GRAPH $image->png or die "Problem writing to image file\n";


I get an error that says "Can't call method 'fetchrow_array' on an undefined value". I checked my temp file and it does in fact contain data. What could be causing this?
 
Um.. can you use fetchrow_array outside of the DBI module? That is the only place I have ever seen it. I don't see it mentioned in IO::File at all.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
and this line looks wrong:

Code:
$datafile->read(file => '$fh', delimiter => '\t');

$fh is an indirect filehandle, wrapping single-quotes around it will treat it as literally $fh and not as a reference to a filehandle. And \t will just be a t in the above, not a tab, maybe you meant to do this:

Code:
$datafile->read(file => $fh, delimiter => "\t");

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
fetchrow_array() is DBI-only. It won't work for regular files. If you need the file in an array, just read it all in at the start.

Aside from this and the stuff Kevin has noted, there seem to be a few other problems. I can't see where $row is getting set prior to the
Perl:
$datafile ->add_point($row);
Suggest you comment out some functionality and substitute it with some print statements to make sure that you actually have the right data in your arrays and variables before you call the graphing module.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top