简体   繁体   English

在Perl中打印二维数组的第一行

[英]print first row of a 2d Array in Perl

I have the below code and I'm attempting to print out only the first row of this 2d array 我有下面的代码,我试图只打印出此二维数组的第一行

# how many columns
for (my $c = 0; $c <= $#list[0]; $c++) {
print $list[0][$c]."\n";

the data should be something like 数据应该像

[0] => "ID,Cluster,Version"
[1] => "2,32,v44"

The Error: 错误:

syntax error at ./connect_qb.pl line 107, near "$#list["
syntax error at ./connect_qb.pl line 107, near "++) "
Execution of ./connect_qb.pl aborted due to compilation errors.
$list[0]

is a reference to an array, so the array is 是对数组的引用,所以该数组是

@{ $list[0] }

so the last element of that array is 所以那个数组的最后一个元素是

$#{ $list[0] }

so you'd use 所以你会用

for my $c (0 .. $#{ $list[0] }) {
   print "$list[0][$c]\n";
}

or 要么

for (@{ $list[0] }) {
   print "$_\n";
}

You should avoid c-style for loops. 您应避免使用c样式的for循环。 Here's one way to do it. 这是一种方法。

use strict;
use warnings;
use feature qw(say);

my @a = (["ID","Cluster","Version"], ["2","32","v44"]);
say for (@{$a[0]});

A slightly less confusing dereferencing: 稍微令人困惑的取消引用:

...
my $ref = $a[0];
say for (@$ref);

这是一个简单的班轮

print join(",",@{$list[0]}),"\n";

Try this: 尝试这个:

for (my $c = 0; $c <=  (scalar @{$list[0]}); $c++) 

for the loop condition 对于循环条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM