在 Perl 中动态创建数组 [英] Creating arrays dynamically in Perl
本文介绍了在 Perl 中动态创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据用户输入动态创建数组.例如,如果用户输入 3,则应创建三个数组,名称分别为 @message1
、@message2
和 @message3
.
I want to create arrays dynamically based on the user input. For example, if the user gives input as 3 then three arrays should be created with the name @message1
, @message2
, and @message3
.
我如何在 Perl 中执行此操作?
How do I do it in Perl?
推荐答案
不要.相反,使用数组数组:
Don't. Instead, use an array of arrays:
my @message;
my $input = 3;
for my $index ( 0..$input-1 ) {
$message[$index][0] = "element 0";
$message[$index][1] = 42;
}
print "The second array has ", scalar( @{ $message[1] } ), " elements\n";
print "They are:\n";
for my $index ( 0..$#{ $message[1] } ) {
print "\t", $message[1][$index], "\n";
}
一些有用的规则位于 http://perlmonks.org/?node=References+quick+参考a>
这篇关于在 Perl 中动态创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文