【问题标题】:How to store variable-arrays inside another array?如何将变量数组存储在另一个数组中?
【发布时间】:2023-03-23 18:59:01
【问题描述】:

我有一个关于将数组保存在另一个数组中的 perl 查询。之前有人问过一个相关问题(How do I add an array ref to the middle of an existing array in Perl?),但我找不到我的答案,所以我在这里发布。

我有 10 个文本文件,每个文件大约有 100 行文本。我想选择所有包含“重要”一词的行。执行此操作的脚本如下。我将所有包含单词“important”的行保存在一个数组中。所以,从每个文本文件中,我得到一个数组。我想将所有这些数组保存在另一个数组中?

my @list_of_files = ("input1.txt", "input2.txt","input3.txt"); my $list_of_files = @list_of_files;

for ($file=0, $file<$list_of_files; $files++){
 open INPUT_FILE, "$list_of_files[$file]" or die "can't open $file : $!";
 my @input = <INPUT_FILE>;
 my $size = @input;

 for ($num=0; $num<$size; $num++){
  if ($input[$num] =~ m/important/) {
   push (@sub_array, $output);
  }
 }
 close INPUT_FILE;
 push (@main_array, \@sub_array);   
}

@sub_array 的元素每次都会改变,那么,如何保留所有 sub_arrays 的元素?我希望最终输出为@main_array,其中包含3个元素,每个元素都是一个元素数组(包含“重要”一词的行)

非常感谢 TIA 的任何帮助

【问题讨论】:

  • 总是在 Perl 文件的开头添加 use strict;use warnings;

标签: perl


【解决方案1】:

我的做法与 bvr 略有不同。但他的观点仍然成立。

# use qw() to define line with less punctuation
my @list_of_files = qw(input1.txt input2.txt input3.txt);

my @lines_in_file;

foreach my $file (@list_of_files) {
  open(my $in, '<', $file) or die "can't open $file : $!";

  # declare an array, not a scalar
  my @lines;

  # idiomatic use of while(<>) puts each record into $_
  while (<$in>) {
    # /../ works on $_ by default.
    # Postfix condition is more readable
    push @lines, $_ if /important/;
  }

  # Take reference to array and push it onto main array
  push @lines_in_file, \@lines;
}

【讨论】:

    【解决方案2】:

    我会这样解决你的问题:

    my @list_of_files = ("input1.txt", "input2.txt", "input3.txt");
    
    my @lines_in_file = ();                 # target array
    for my $file (@list_of_files) {
        open(my $in,'<',$file) or die "can't open $file : $!";
    
        my $lines = [];                     # scalar containing arrayref for important lines
        while(my $input = <$in>) {
            if($input =~ /important/) {
                push @$lines, $input;       # push into arrayref
            }
        }
        close $in;
    
        push @lines_in_file, $lines;        # push arrayref into main array
    }
    

    注意事项:

    • perl for 可以遍历数组,无需 C 样式 for 的麻烦
    • 最好逐行处理文件,而不是将整个文件读入内存,尤其是基于行的处理
    • [] 在每次迭代中构造新的 arrayref。如果你在每个循环中重复使用 @sub_array 并引用它,你最终会得到很多对同一事物的引用

    【讨论】:

    • 我更喜欢use autodie; 而不是open ... or die ...,尤其是在 StackOverflow 的答案上。
    • 同样while( my $input = &lt;$in&gt; ){ ... } 会自动转换为while( defined( my $input = &lt;$in&gt; ) ){ ... }
    • @Brad Gilbert - 我根据您的建议更改了 while 循环,我不知道此功能。我知道并使用autodie,但它增加了一个要收集的概念,如果您不了解它,则很难从open 电话中理解。
    【解决方案3】:

    如果您要时不时地用 Perl 编写一些东西,您将必须了解引用的工作原理以及如何构建复杂的数据结构。

    作为 Perl 新手,我建议您访问 perldoc.perl.orgmanuals section,并完成前三节课。只需大约一个半小时,您就会对如何解决您的问题和其他类似问题有一个想法。

    tutorials 非常有用 - 简短实用。您将学习传递数据结构并对其进行迭代等等。

    对于测试,请考虑使用Data::Dumperprint Dumper($var) 轻松查看您复杂的数据结构。

    【讨论】:

    • @Brad,感谢您的编辑,下次我会尝试自己提供链接,但是从 android 设备编写时会很乏味。 %)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多