我如何在perl中访问array的数组? [英] How do I access arrays of array in perl?

查看:419
本文介绍了我如何在perl中访问array的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组作为myarray.我想列出一个"1 2 3",该列表正在连接第一个子数组.我的字符串正在打印我假定的存储位置,而不是列表.任何帮助将不胜感激.

Hi I have a array as myarray. I would like to make a list as '1 2 3' which is joining the first subarray. My string is printing the memory location I suppose instead of list. any help will be appreciated.

@myarray = [[1,2,3],[4,5,6],[7,8,9]];
for (my $i=0; $i < @myarray; $i++) {
my @firstarray = $myarray[$i];
my $mystring = join("", @firstarray);
print "My string ".$mystring . ". "\n";
}

推荐答案

您必须通过@{ ... }取消引用内部数组引用.另外,请勿将[...]用于顶部结构-使用常规括号(方括号创建数组引用,而不是数组). print行上的串联也存在问题:

You have to dereference the inner array reference by @{ ... }. Also, do not use [...] for the top structure - use normal parentheses (square brackets create an array reference, not an array). There was also a problem with the concatenation on your print line:

@myarray = ( [1,2,3], [4,5,6], [7,8,9] );
for (my $i=0; $i < @myarray; $i++) {
    my @firstarray = @{ $myarray[$i] };
    my $mystring = join("", @firstarray);
    print "My string " . $mystring . ".\n";
}

这篇关于我如何在perl中访问array的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆