[英]Create schedule as a 2D array perl
I'm trying to initialise a 2D array in Perl that is basically a schedule. 我正在尝试在Perl中初始化一个基本上是时间表的2D数组。 So it looks like this: 所以它看起来像这样:
Time Mon Tue Wed Thu Fri
09:00 0 0 0 0 0
10:00 0 0 0 0 0
11:00 0 0 0 0 0
12:00 0 0 0 0 0
13:00 0 0 0 0 0
14:00 0 0 0 0 0
15:00 0 0 0 0 0
16:00 0 0 0 0 0
17:00 0 0 0 0 0
18:00 0 0 0 0 0
19:00 0 0 0 0 0
20:00 0 0 0 0 0
I read some articles on 2D arrays in Perl but I'm still confused. 我在Perl上阅读了一些有关2D数组的文章,但我仍然感到困惑。 What's the easiest way to do this? 最简单的方法是什么? And please explain a bit so I can get what's going on. 请解释一下,这样我就能得到正在发生的事情。
Take a look at perllol
. 看看perllol
。 It is the best documentation for lists of lists in Perl 它是Perl中列表列表的最佳文档
Essentially, each element of a Perl array can be any scalar value - a string or number, or something more exotic like a subroutine reference or a filehandle reference. 从本质上讲,Perl数组的每个元素都可以是任何标量值 - 字符串或数字,或者像子例程引用或文件句柄引用更具奇异性的东西。 To create a 2D array you want an array of arrays, so each element of the base array needs to be an array reference 要创建一个2D数组,您需要一个数组数组,因此基数组的每个元素都需要是一个数组引用
The [...]
construct creates an anonymous array and returns a reference to it. [...]
构造创建一个匿名数组并返回对它的引用。 The anonymous part just means that it has no identifier attached to it like @data
, but it exists in memory and can be accessed through its reference 匿名部分只是意味着它没有附加到它的标识符,如@data
,但它存在于内存中,可以通过其引用访问
The syntax for accessing an element of an array through a reference is, for instance, $array_ref->[0]
for the first element. 例如,通过引用访问数组元素的语法是第一个元素的$array_ref->[0]
。 So if I write 所以,如果我写
my $array_ref = [ '09:00', 0, 0, 0, 0, 0 ];
print $array_ref->[0];
I would see 09:00
output 我会看到09:00
输出
For your data, you can write 对于您的数据,您可以编写
my @data = (
[ '09:00', 0, 0, 0, 0, 0 ],
[ '10:00', 0, 0, 0, 0, 0 ],
[ '11:00', 0, 0, 0, 0, 0 ],
[ '12:00', 0, 0, 0, 0, 0 ],
[ '13:00', 0, 0, 0, 0, 0 ],
[ '14:00', 0, 0, 0, 0, 0 ],
[ '15:00', 0, 0, 0, 0, 0 ],
[ '16:00', 0, 0, 0, 0, 0 ],
[ '17:00', 0, 0, 0, 0, 0 ],
[ '18:00', 0, 0, 0, 0, 0 ],
[ '19:00', 0, 0, 0, 0, 0 ],
[ '20:00', 0, 0, 0, 0, 0 ],
);
So $data[0]
is a reference to an (anonymous) array holding the first line of data, and $data[0]->[0]
is '09:00'
所以$data[0]
是对包含第一行数据的(匿名)数组的引用, $data[0]->[0]
是'09:00'
In addition there is a syntax facility that allows the arrow between any pair of closing and opening brackets to be omitted, so that $data[0]->[0]
may be written as $data[0][0]
with exactly the same meaning 此外,还有一个语法工具,允许省略任何一对关闭和左括号之间的箭头,以便$data[0]->[0]
可以写为$data[0][0]
相同的含义
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.