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

Hi monks, I am trying to foreach through a data-file, retreive key values and form a graph with these values (one graph per line, each should turn out different). The problem is that when i print the graphs within the loop, the same single graph is being printed for each line and I think the program is only actually creating one graph each time it is ran, (not individual graphs). I tested the values in each array that are being passed to make the graph - and these are different for each line. I hope someone can help!
my @data = split /\n/, $data; $data[2] =~ s/,/\t/g; my @temps = $data[2]; my $temps = join ('', @temps); @temps = split (/\s+/, $temps); shift @temps; foreach my $line (@file) { @new = (); push (@new, $+) while $line =~ m { "([^\"\\]*(?:\\.[^\"\\]*)*)",? | ([^,]+),? |, }gx; push (@new, undef) if substr ($line, -1,1) eq ','; join('', split(/(?=\w)/, @new)); shift @new; # printing @temps and @new here prints different values for e +ach line (of @new) # prints same graph each time my $plot = plot_graph (\@temps, \@new); print qq(<img src=$plot>); } sub plot_graph { my ($temps, $numbers) = @_; my @graph_data = (\@$temps, \@$numbers); my $graph = GD::Graph::lines ->new (600,600); $graph->set( x_label => 'Temperature degrees C', y_label => 'Fluorescence Derivative', x_max_value =>95, y_max_value =>0.7, x_label_position =>1/2, x_tick_number =>20, y_tick_number =>10 ) or die $graph->error; $graph->set (dclrs=> [qw(green red blue)]); my $gb = $graph->plot(\@graph_data) or die $graph- +>error; open (IMG , ">temp/$$.png") or die $!; binmode IMG; print IMG $gb->png; close IMG; my $plot = "temp/$$.png"; return $plot; }

Replies are listed 'Best First'.
Re: loops + creating graphs
by BrowserUk (Patriarch) on May 28, 2003 at 16:22 UTC

    Each time you produce a new graph, your writing it to the same file!


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      Do you know how I can get around this??

        Use different file names:)

        See Automatically creating incremental file names for recent discussion on this very problem.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


        i think what you want to do is create a temporary file; that way, you can store the plot data in it. and not have it overwritten.

        according to the cookbook, you could use tmpnam. there was a posting with that in the last couple of days (check "newest nodes"). i prefer File::Temp.

        something like
        use File::Temp; my $tfname = File::Temp::tempnam($tmpdir, "math.")
        will get you *the name* for a create a temporary file with the prefix "math". you'll have to write the stuff in the loop to it yourself.

        ...wufnik -- in the world of the mules there are no rules --