好吧,不知道在哪里问这个,但我是初学程序员,使用Perl.我需要创建一个数组的数组,但我不确定是否更好地使用数组/哈希引用,哈希数组或哈希数组等.
我需要一系列匹配:@totalmatches
每个匹配包含6个元素(字符串):
@matches = ($chapternumber,$sentencenumber,$sentence,$grammar_relation,$argument1,$argument2)
我需要将每个元素推送到@matches数组/散列/引用中,然后将该数组/散列/引用推送到@totalmatches数组中.
质询
>您将使用哪种数据结构?
>您可以将数组推入另一个数组,就像将元素推入数组一样吗?这是一种有效的方法吗?
>您可以同时推动所有6个元素,还是必须进行6次单独推送?
>使用2-D时,要循环使用:
foreach (@totalmatches) {
foreach (@matches) {
…
}
}
谢谢你的建议.
解决方法
不要将一个数组推入另一个数组.
列表只是相互加入一个新列表.
列表只是相互加入一个新列表.
使用参考列表.
#create an anonymous hash ref for each match $one_match_ref = { chapternumber => $chapternumber_value,sentencenumber => $sentencenumber_value,sentence => $sentence_value,grammar_relation => $grammar_relation_value,arg1 => $argument1,arg2 => $argument2 }; # add the reference of match into array. push @all_matches,$one_match_ref; # list of keys of interest @keys = qw(chapternumber sentencenumber sentence grammer_relation arg1 arg2); # walk through all the matches. foreach $ref (@all_matches) { foreach $key (@keys) { $val = $$ref{$key}; } # or pick up some specific keys my $arg1 = $$ref{arg1}; }