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

标签 perl

好吧,不知道在哪里问这个,但我是一个初学者程序员,使用 Perl。我需要创建一个数组数组,但我不确定使用数组/哈希引用、哈希数组或数组哈希等是否更好。

我需要一组匹配项:@totalmatches
每个匹配包含 6 个元素(字符串):

@matches = ($chapternumber, $sentencenumber, $sentence, $grammar_relation, $argument1, $argument2)

我需要将这些元素中的每一个放入 @matches数组/散列/引用,然后将该数组/散列/引用插入 @totalmatches大批。

匹配是基于搜索文件并根据满足条件选择字符串来找到的。

问题
  • 你会使用哪种数据结构?
  • 您可以将一个数组插入另一个数组,就像将一个元素插入一个数组一样吗?这是一种有效的方法吗?
  • 您可以同时推送所有 6 个元素,还是必须执行 6 个单独的推送?
  • 使用 2-D 时,要循环使用,您会使用:

    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/

    相关文章:

    linux - 如何阻止 Eclipse (EPIC Perl) 将 DOS CR/LF 添加到我的 Perl 脚本中?

    perl - 定义了一些字符串值 ("0"、 ""和 ""),但是 (==undef)

    perl - 如何将散列的一部分传递给子例程?

    perl - Catalyst 开发服务器 - 不显示路由和错误

    perl - 使用哈希值访问哈希值

    perl - 在 perl 中使用 stat 时 $mode 保存什么

    android - Aosp 构建错误

    database - Class::DBI 的 future 是什么?

    Perl:一行中有多个 = 符号(列表和标量上下文混淆)

    perl - DBM 文件可由生成该文件的计算机上的 Perl 脚本读取,但在其他计算机上为 "Inappropriate file type or format"