加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl:初学者.我应该使用哪种数据结构?

发布时间:2020-12-15 23:33:18 所属栏目:大数据 来源:网络整理
导读:好吧,不知道在哪里问这个,但我是初学程序员,使用Perl.我需要创建一个数组的数组,但我不确定是否更好地使用数组/哈希引用,哈希数组或哈希数组等. 我需要一系列匹配:@totalmatches 每个匹配包含6个元素(字符串): @matches = ($chapternumber,$sentencenumber
好吧,不知道在哪里问这个,但我是初学程序员,使用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};
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读