3

我正在编写一个程序,该程序接受用户的输入文件。该文件中有一堆数字,我将读取文件中的数字并使用GD::Graph根据这些数字创建一个图。

文件的第一行是X轴,文件的第二行是X轴对应的Y值和第三、第四、...等例如:

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

所以在上面,第一行是 x 轴,第二行是对应于 xaxis 的 y 值,所以这将获取 10 个点。(1, 2) (1, 5) (2, 4) (2, 6)....(4,10) (4,12) (5,14) (5, 13)

我计划读取数组的每一行,然后将行拆分为空格或制表符并将值存储在数组中。因此,数组 1 将具有 x 轴,数组 2 将具有 y 轴,但是我应该如何在数组中存储第 3、第 4、第 5、... 等行,以便它们成为(x,y)?

此外,如何找到第一行和第二行(2 个数组)的最大值,以便为 X 轴和 Y 轴创建限制?

4

3 回答 3

2

不是您问题的真正答案,但不要错过GD::Graph::Data

除非您确定每行的第一个和最后一个是最小/最大的,否则您需要使用List::Utilmin()max().


我不太明白您所说的“文件的第一行是 X 轴,文件的第二行是 Y 轴,第三,第四,...等是 X 轴的对应点”是什么意思。

于 2009-04-03T16:11:40.037 回答
1

您可以随时扩展您的 x 和 y 数组。

#!/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;
}
于 2009-04-03T17:47:30.760 回答
0

哎呀,误读了这个问题,您想要 AoAoH 还是 AoH,这取决于第一条线之后的每条线是代表一条线,还是都只是要分别绘制的点。如果文件中的每一行都成为图中的一行,我会这样写:

#!/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";
}

如果它们只是要绘制的点,我将如何处理它:

#!/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";
于 2009-04-03T16:02:43.350 回答