Hello buchi2

I seems there is a bit of confusion, on what you are asking and what you want to create at the end.

Let's take it one step at a time. You want to open a file that contains data in CSV format. Fellow Monks haukex, flightdm and dbander have provided you with examples on how to do that.

The reason is that you asked for (@histofeld) = split(',',$input[$i]); which is not what you need. From the documentation of the module GD::Graph::histogram/USAGE you can see $data = [1,5,7,8,9,10,11,3,3,5,5,5,7,2,2]; that the data need to populated in array reference format and not a simple array. Read more here on perldoc/Making-References.

As a next step you wrote But now I have to plot several histograms and the data is coming each histogrammdata one line from a file. So I decided to take it a bit further and write a more generic script for you. What the script does, it takes an n number of files (CSV format) process them and create an Hash of Hashes of Arrays data structure, read about complex data structures here perldsc - Perl Data Structures Cookbook. Then the second step is simply read the data from the complex data structure and populate the graphs based on each line. Each graph name is based on the input file name and line number. You can play around with the name as you wish to modify them.

Sample of code, sample of data and sample of output:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use GD::Graph::histogram; sub createGraphs { my ( $HoHoA ) = @_; for my $hashRef ( keys %$HoHoA ) { for my $line ( sort keys %{ $HoHoA->{$hashRef} } ) { my $graph = new GD::Graph::histogram(400,600); $graph->set( x_label => 'X Label', y_label => 'Count', title => "Histogram From: $hashRef line number +$line", x_labels_vertical => 1, bar_spacing => 0, shadow_depth => 1, shadowclr => 'dred', transparent => 0, ) or warn $graph->error; my $gd = $graph->plot($HoHoA->{$hashRef}{$line}) or die $graph->error; open(my $IMG, ">", substr($hashRef, 0, -4).'_'.$line.'.png') or die "Unable to save the graph: $!"; binmode $IMG; print $IMG $gd->png; close($IMG) or die "Unable to close image save: $!"; } } return; } my %HoHoA; while (<>) { chomp; push @{ $HoHoA{$ARGV}{$.} }, split(/,/, ) if (index($_, ',') != -1); } continue { close ARGV if eof; } # print Dumper \%HoHoA; # view the complex structure createGraphs( \%HoHoA ); __DATA__ ~/PerlMonks$ cat data.txt 1,5,7,8,9,10,11,3,3,5,5,5,7,2,2 2,6,7,8,9,15,17,3,4,5,6,5,7,2,3 __END__ ~/PerlMonks$ ls -la | grep data -rw-r--r-- 1 user user 2080 Sep 1 01:48 data_1.png -rw-r--r-- 1 user user 2015 Sep 1 01:48 data_2.png -rw-r--r-- 1 user user 64 Sep 1 01:47 data.txt

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: How to get data to an array? by thanos1983
in thread How to get data to an array? by buchi2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.