简体   繁体   English

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

[英]Perl: Beginner. Which data structure should I use?

Okay, not sure where to ask this, but I'm a beginner programmer, using Perl.好的,不知道在哪里问这个,但我是一个初学者程序员,使用 Perl。 I need to create an array of an array, but I'm not sure if it would be better use array/hash references, or array of hashes or hash of arrays etc.我需要创建一个数组的数组,但我不确定使用数组/哈希引用、哈希数组或 arrays 等的 hash 是否会更好。

I need an array of matches: @totalmatches我需要一组匹配项: @totalmatches

Each match contains 6 elements(strings):每个匹配包含 6 个元素(字符串):

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

I need to push each of these elements into the @matches array/hash/reference, and then push that array/hash/reference into the @totalmatches array.我需要将这些元素中的每一个推入@matches数组/哈希/引用,然后将该数组/哈希/引用推入@totalmatches数组。

The matches are found based on searching a file and selecting the strings based on meeting the criteria.基于搜索文件并根据满足条件选择字符串来找到匹配项。

QUESTIONS问题

  1. Which data structure would you use?你会使用哪种数据结构?

  2. Can you push an array into another array, as you would push an element into an array?你可以将一个数组推入另一个数组,就像你将一个元素推入一个数组一样吗? Is this an efficient method?这是一种有效的方法吗?

  3. Can you push all 6 elements simultaneously, or have to do 6 separate pushes?您可以同时推送所有 6 个元素,还是必须进行 6 次单独推送?

  4. When working with 2-D, to loop through would you use:在使用 2-D 时,要循环使用,您会使用:

    foreach (@totalmatches) { foreach (@matches) {... } } foreach (@totalmatches) { foreach (@matches) {... } }

Thanks for any advice.感谢您的任何建议。

Which data structure would you use?你会使用哪种数据结构?

An array for a ordered set of things.一组有序事物的数组。 A hash for a set of named things.一组命名事物的 hash。

Can you push an array into another array, as you would push an element into an array?你可以将一个数组推入另一个数组,就像你将一个元素推入一个数组一样吗? Is this an efficient method?这是一种有效的方法吗?

If you try to push an array (1) into an array (2), you'll end up pushing all the elements of 1 into 2. That is why you would push an array ref in instead.如果您尝试将数组 (1) 推入数组 (2),最终会将 1 的所有元素推入 2。这就是为什么您将推入数组 ref 的原因。

Can you push all 6 elements simultaneously, or have to do 6 separate pushes?您可以同时推送所有 6 个元素,还是必须进行 6 次单独推送?

Look at perldoc -f push看看perldoc -f push

push ARRAY,LIST

You can push a list of things in.你可以推入列表。

When working with 2-D, to loop through would you use:在使用 2-D 时,要循环使用,您会使用:

Nested foreach is fine, but that syntax wouldn't work.嵌套的 foreach 很好,但这种语法不起作用。 You have to access the values you are dealing with.您必须访问您正在处理的值。

for my $arrayref (@outer) {
    for my $item (@$arrayref) {
        $item ...
    }
}

Do not push one array into another array.不要将一个阵列推入另一个阵列。 Lists just join with each other into a new list.列表只是相互连接成一个新列表。

Use list of references.使用参考列表。

#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};
}

Which data structure would you use?你会使用哪种数据结构?

An array... I can't really justify that choice, but I can't imagine what you would use as keys if you used a hash.一个数组...我无法真正证明该选择的合理性,但我无法想象如果您使用 hash,您会使用什么作为键。

Can you push an array into another array, as you would push an element into an array?你可以将一个数组推入另一个数组,就像你将一个元素推入一个数组一样吗? Is this an efficient method?这是一种有效的方法吗?

Here's the thing;事情是这样的; in Perl, arrays can only contain scalar variables - the ones which start with $ .在 Perl 中,arrays 只能包含标量变量 - 以$开头的变量。 Something like...就像是...

@matrix = ();
@row = ();
$arr[0] = @row; # FAIL!

... wont't work. ......不会工作。 You will have to instead use a reference to the array:您将不得不使用对数组的引用:

@matrix = ();
@row = ();
$arr[0] = \@row;

Or equally:或同样:

push(@matrix, \@row);

Can you push all 6 elements simultaneously, or have to do 6 separate pushes?您可以同时推送所有 6 个元素,还是必须进行 6 次单独推送?

If you use references, you need only push once... and since you don't want to concatenate arrays (you need an array of arrays) you're stuck with no alternatives;)如果你使用引用,你只需要推送一次......因为你不想连接 arrays (你需要一个数组数组)你没有其他选择;)

When working with 2-D, to loop through would you use:在使用 2-D 时,要循环使用,您会使用:

I'd use something like:我会使用类似的东西:

for($i=0; $i<@matrix; $i++) {
    @row = @{$matrix[$i]}; # de-reference
    for($j=0; $j<@row; $j++) {
        print "| "$row[$j];
    }
    print "|\n";
}

Which data structure would you use?

Some fundamental container properties:一些基本的容器属性:

  • An array is a container for ordered scalars.数组是有序标量的容器。

  • A hash is a container for scalars obtained by a unique key (there can be no duplicate keys in the hash). hash 是通过唯一键获得的标量的容器(哈希中不能有重复的键)。 The order of values added later is not available anymore.稍后添加的值的顺序不再可用。

I would use the same structure like ZhangChn proposed.我会使用与 ZhangChn 提出的相同的结构。

Use a hash for each match.每场比赛使用 hash。 The details of the match then can be accessed by descriptive names instead of plain numerical indices.然后可以通过描述性名称而不是简单的数字索引来访问匹配的详细信息。 ie $ref->{'chapternumber'} instead of $matches[0] .$ref->{'chapternumber'}而不是$matches[0]

Take references of these anonymous hashes (which are scalars) and push them into an array in order to preserve the order of the matches.引用这些匿名哈希(它们是标量)并将它们推送到数组中以保持匹配的顺序。

To dereference items from the data structure从数据结构中取消引用项目

  • get an item from the array which is a hash reference从数组中获取一个项目,该项目是 hash 参考

  • retrieve any matching detail you need from the hash reference从 hash 参考中检索您需要的任何匹配细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM