如果你想要第 n 列空格分隔的字符串,这就是如何做到这一点的想法:
#!/usr/bin/env perl
use strict;
use warnings;
my @foo = ( "Col1 Col2 Col3 Col4", # This is
"Row2 R2C2 Row2C3 Row2C4" ); # the input array.
my $n = 2; # We want to select the 3rd column.
my @nth_columns;
for my $row (@foo) { # We go through the input array,
my @columns = split /\s+/, $row; # splitting each row by whitespaces
push @nth_columns, $columns[$n]; # and adding the n-th column to output array
}
你当然可以用许多更短的方式来写。我最喜欢的应该是这样的:
my @third_columns = map { (split /\s+/)[2] } @foo;