哎呀,误读了这个问题,您想要 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";