in reply to How to get data to an array?

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!