views:

274

answers:

3

I am writing a program that takes in an input file from the user. The file has bunch of numbers in it and I will read the numbers in the file and create a plot based on those numbers using GD::Graph.

The first line of the file is X axis, second line of the file is Y values corresponding to X axis and third, fourth, ..., etc For example:

1 2 3 4 5 
2 4 5 10 14
5 6 8 12 13

So in the above, first line is x-axis, second is y values corresponding to xaxis so this will fetch 10 points. (1, 2) (1, 5) (2, 4) (2, 6)....(4,10) (4,12) (5,14) (5, 13)

I plan on reading each line of the array and then splitting the line on spaces or tabs and storing the values in an array. So, array 1 will have x-axis, array2 will have y-axis, but how should I store 3rd, 4th, 5th, ..., etc lines in an array so they become (x,y)?

Furthermore, how can I find the largest value for the first and second lines (2 arrays) so I can create a limit for my X and Y axes?

A: 

Whoops, misread the question, you want either an AoAoH or an AoH, depending on whether each line after the first represents a line or the are all just points to be plotted respectively. Here is how I would write it if each line in the file was to become a line in the graph:

#!/usr/bin/perl

use strict;
use warnings;
use List::Util qw/min max/;

my @x_points         = split " ", scalar <>; #read in the x axis labels
my ($x_min, $x_max)  = (sort { $a <=> $b } @x_points)[0,-1];
my ($y_min, $y_max)  = (0, 0);

#lines is an AoAoH, first layer are the lines to be drawn
#second layer is a list of coords
#third layer are the x and y coords
my @lines;
while (<>) {
    my @y_points = split;
    #if the two arrays are not the same size, we have a problem
    die "invalid file\n" unless @y_points == @x_points;

    $y_min = max($y_min, @y_points);
    $y_max = min($y_max, @y_points);

    push @lines, [ 
        map { { x => $x_points[$_], y => $y_points[$_] } }  
    0 .. $#x_points 
    ];
}

use Data::Dumper;

print "x min and max $x_min $x_max\n",
      "y min and max $y_min $y_max\n",
      "data:\n",
      Dumper(\@lines);

my $i;
for my $line (@lines) {
    $i++;
    print "line $i is made up of points: ",
        (map { "($_->{x}, $_->{y}) " } @$line), "\n";
}

And here is how I would handle it if they are just points to be ploted:

#!/usr/bin/perl

use strict;
use warnings;
use List::Util qw/min max/;

my @x_points         = split " ", scalar <>; #read in the x axis labels
my ($x_min, $x_max)  = (sort { $a <=> $b } @x_points)[0,-1];
my ($y_min, $y_max)  = (0, 0);

#lines is an AoAoH, first layer are the lines to be drawn
#second layer is a list of coords
#third layer are the x and y coords
my @points;
while (<>) {
    my @y_points = split;
    #if the two arrays are not the same size, we have a problem
    die "invalid file\n" unless @y_points == @x_points;

    $y_min = max($y_min, @y_points);
    $y_max = min($y_max, @y_points);

    push @points,
        map { { x => $x_points[$_], y => $y_points[$_] } }
        0 .. $#x_points;
}

use Data::Dumper;

print "x min and max $x_min $x_max\n",
      "y min and max $y_min $y_max\n",
      "data:\n",
      Dumper(\@points);

print "Here are the points: ", 
    (map { "($_->{x}, $_->{y}) " } @points), "\n";
Chas. Owens
I am new to perl, so need some clarification on some points: if you are saying first and last elements will be outliers then are you assuming that the data inputted by the user is sorted?? will AoA contain all the data excluding x and y axis??
The x and y axis labels should be sorted (otherwise they aren't axes), the AoA only contains the points to plot.
Chas. Owens
@scotty- Some helpful tutorials are: the Data Structures Cookbook http://perldoc.perl.org/perldsc.html, the Array of Arrays Tutorial http://perldoc.perl.org/perllol.html, and the Reference Tutorial http://perldoc.perl.org/perlreftut.html.
daotoad
@scotty- Also, the perl documentation is a bit overwhelming in the beginning (there is so much to read), I found this article on PerlMonks very helpful: http://www.perlmonks.org/?node=How%20to%20RTFM
daotoad
+2  A: 

Not really an answer to your question, but don't miss GD::Graph::Data.

Unless you are sure that the first and last on each line is smallest/biggest you'll need to use something like List::Util's min() and max().


I don't really understand what you mean by "The first line of the file is X axis, second line of the file is Y axis and third, fourth, ..., etc are corresponding points to X axis."

Anon
I mnt: The first set of ten values will be the x values for the graph. These will be qually spaced e.g. 0, 1, 2, 3, ..., 9The second set of ten values will be the y values corresponding to first 10 x values, e.g. 20, 31, 42, 5, 7, ...This example yields the points (0,20), (1,31), (2,42), etc.
i see what you meant..that was misleading, I've edited the question now
+1  A: 

You can grow your x and y arrays as you go.

#!/usr/bin/perl

use Data::Dumper;
use warnings;
use strict;

my @xs = ();
my @ys = ();
my $expecting_xs = 1;
my $last_xs_count;

while(<>) {
  chomp;
  my @values = split(/\s+/);
  if($expecting_xs) {
    push(@xs, @values);
    $last_xs_count = @values;
    $expecting_xs = 0;
  } else {
    if(@values != $last_xs_count) {
      die "Count mismatch";
    } 
    push(@ys, @values);
    $expecting_xs = 1;
  }
}

if(!$expecting_xs) {
  die("Odd number of lines");
}

my($xmin, $xmax) = extremes(@xs);
my($ymin, $ymax) = extremes(@ys);

print "xmin: $xmin xmax: $xmax ymin: $ymin ymax: $ymax\n";
print Dumper(\@xs), Dumper(\@ys);

sub extremes {
  my(@values) = @_;
  return undef unless @values;
  my $min = shift(@values);
  my $max = $min;
  for my $value (@values) {
    $max = $value if $value > $max;
    $min = $value if $value < $min;
  }
  return $min, $max;
}
Glomek