GHMON has asked for the wisdom of the Perl Monks concerning the following question:

Hi i have problem when i run this code , this should show me a graph but !!!!!

use warnings ; use strict ; use GD::Graph::linespoints ; use Text::Flowchart ; my %position = ( 'a' => "0 , 0" , 'b' => "1 , 0" , 'c' => "2 , 0" , 'd' => "0 , 1" , 'e' => "1 , 1" , 'f' => "2 , 1" , 'g' => "0 , 2" , 'h' => "1 , 2" , 'i' => "0 , 3" , 'k' => "1 , 3" , 'l' => "2 , 3" , ) ; my @p = keys %position ; my @v = values %position ; #my @p = ('$a','$b','$c','$d','$e','$f','$g','$h','$i','$k','$l') ; my $fee ; my @point ; my $lastfee = 0 ; my $timer = 0 ; my $request = 0 ; my $tvacume = 2 ; while ($request < 180) { my $pdirty = $p [int rand @p] ; my $pvacum = $p [int rand @p] ; my @p2 = split (/\,/, ($position{"$pdirty"})) ; my @p1 = split (/\,/, ($position{"$pvacum"})) ; #print $p1[0] , $p1[1] , "\n" ; #print $p2[0] , $p2[1] , "\n" ; if ($pvacum eq $pdirty) { print 'Fee is Zero' , "\n" ; $fee = 0 ; } else { $fee = 2 * (abs (($p2[0] - $p1[0]) + ($p2[1] - $p1[1]))) ; } $timer = $timer + ($fee + 2) ; $pvacum = $pdirty ; print $request , "\n" ; #print "$fee" , ":" , "$timer" , "\n\n" ; $request++ ; #sleep (28.8) ; $lastfee = $fee + $lastfee ; push @point , \$timer , \$fee ; print $lastfee , "\t\t" ; #print @point , "\n" ; } my $graph = new GD::Graph::linespoints(500 , 500) ; $graph->set( x_label => 'Time' , y_label => 'Fee' , title => 'Time Vs Fee') or warn $graph->error ; my $diagram = GD::Graph::Data->new(@point) ; print ($graph->plot(\@point)) , "\n" ; #print header(-type=>'image/jpg') , $graph->gd->jpeg ;

Replies are listed 'Best First'.
Re: print a graph
by poj (Abbot) on Oct 28, 2018 at 15:36 UTC

    You need to transpose your @point array so that $point[0] is the series of times and $point[1] is the series of fees

    push @{$point[0]},$timer; push @{$point[1]},$fee;

    try

    #!/usr/bin/perl use warnings ; use strict ; use GD::Graph::linespoints ; my %position = ( 'a' => [0,0] , 'b' => [1,0] , 'c' => [2,0] , 'd' => [0,1] , 'e' => [1,1] , 'f' => [2,1] , 'g' => [0,2] , 'h' => [1,2] , 'i' => [0,3] , 'k' => [1,3] , 'l' => [2,3] , ) ; my @p = keys %position ; my @point ; my $timer = 0 ; for my $request (0..179) { my $pdirty = $p[int rand @p] ; my $pvacum = $p[int rand @p] ; my $fee = 0; if ($pvacum eq $pdirty) { print 'Fee is Zero' , "\n" ; } else { my @p2 = @{$position{$pdirty}} ; my @p1 = @{$position{$pvacum}} ; $fee = 2 * (abs (($p2[0] - $p1[0]) + ($p2[1] - $p1[1]))) ; } $timer = $timer + $fee + 2 ; push @{$point[0]},$timer; push @{$point[1]},$fee; print "$request\t$timer\t$fee\n" ; } my $graph = new GD::Graph::linespoints(1000 , 500) ; $graph->set( x_label => 'Time' , x_label_skip => 5, y_label => 'Fee' , title => 'Time Vs Fee') or warn $graph->error ; $graph->plot(\@point); open OUT,'>','image.jpeg' or die "$!"; binmode OUT; print OUT $graph->gd->jpeg;
    poj

      TNX BRO

      you help me :)))))))))

      it worked

Re: print a graph
by Athanasius (Archbishop) on Oct 29, 2018 at 02:49 UTC

    Hello GHMON,

    As a side note: the code shown generates two warning messages:

    12:40 >perl 1936_SoPW.pl print (...) interpreted as function at 1936_SoPW.pl line 74. Useless use of a constant ("\n") in void context at 1936_SoPW.pl line +74.

    The correction is to change the line:

    print ($graph->plot(\@point)) , "\n" ;

    to

    print +($graph->plot(\@point)) , "\n" ;

    or, more simply,

    print $graph->plot(\@point), "\n" ;

    See print:

    Be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print; put parentheses around all arguments (or interpose a +, but that doesn't look as good).

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,