我写一个程序,它在来自用户的输入文件。该文件中有一串数字中,而我会在文件基础上通过的 GD ::格拉夫

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

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

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

我打算在阅读该阵列的每一行,然后对分割空格或标签上的线和在阵列中存储的值。所以,阵列1将具有x轴,数组2将具有y轴,但如何我应该存储第三,第四,第五,...,等线的阵列,使他们成为(X,Y)

此外,如何可以找到用于第一和第二线(2个阵列)的最大值,所以我可以为我的X创建的限制和Y轴?

有帮助吗?

解决方案

糟糕,误读的问题,要任一种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";
}

这是我将如何处理它,如果他们只是指出要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";

其他提示

不是一个真正的回答你的问题,但千万不要错过 GD: :格拉夫::数据

除非你的确定的第一个和最后每行最小/最大,你需要使用类似的一览::的Util min()max()


我真的不明白你的意思是“该文件的第一行是X轴,文件的第二行是Y轴,第三,第四,...等等都对应点X轴。”

当您去,您可以增加您的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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top