好吧,不知道在哪里问这个,但我是一个初学者程序员,使用 Perl。我需要创建一个数组数组,但我不确定使用数组/哈希引用、哈希数组或数组哈希等是否更好。
我需要一组匹配项:@totalmatches
每个匹配包含 6 个元素(字符串):
@matches = ($chapternumber, $sentencenumber, $sentence, $grammar_relation, $argument1, $argument2)
我需要将这些元素中的每一个放入
@matches
数组/散列/引用,然后将该数组/散列/引用插入 @totalmatches
大批。匹配是基于搜索文件并根据满足条件选择字符串来找到的。
问题
foreach (@totalmatches) { foreach (@matches) { ... } }
感谢您的任何建议。
最佳答案
Which data structure would you use?
一组有序事物的数组。一组命名事物的散列。
Can you push an array into another array, as you would push an element into an array? Is this an efficient method?
如果您尝试将数组 (1) 推送到数组 (2),您最终会将 1 的所有元素推送到 2。这就是为什么要推送数组 ref 的原因。
Can you push all 6 elements simultaneously, or have to do 6 separate pushes?
看
perldoc -f push
push ARRAY,LIST
你可以插入一个列表。
When working with 2-D, to loop through would you use:
嵌套的 foreach 很好,但该语法不起作用。您必须访问您正在处理的值。
for my $arrayref (@outer) {
for my $item (@$arrayref) {
$item ...
}
}
关于Perl:初学者。我应该使用哪种数据结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6298895/