在Perl中找到匹配的模式后,将下一行放入数组中

时间:2012-10-24 00:01:22

标签: perl

我在 Perl脚本中打开了一个文本报告,需要找到特定的行并将它们存储在数组中。

this is my report which I need to process through:
matched pattern 1 
line1:10
line2:20
line3:30

next matched pattern 2
line1:5
line2:10
line3:15

next matched pattern 3
lineA:A
lineB:B
lineC:C
.
.
------------------------------------

这部分是我的剧本:

@numbers;
@numbers2;
@letters;
while (<FILE>)
{
 if ($_ =~/matched pattern 1/ && $_ ne "\n")
 {
   chomp();
   push (@numbers,$_)
 }
 if ($_ =~/next matched pattern 2/ && $_ ne "\n")
 {
   chomp();
   push (@numbers2,$_)
 }
 if ($_ =~/next matched pattern 3/ && $_ ne "\n")
 {
   chomp();
   push (@letters,$_)
 }
}

然后我可以在数组中使用数字和字母。 这是我的报告文件的一部分

Maximum points per Lab
Lab1:10
Lab2:30
Lab3:20


Maximum points per Exam
Exam1:50
Exam2:50

Maximum points on Final
Final:150

3 个答案:

答案 0 :(得分:1)

@numbers;
@letters;
open FILE, "report2.txt" or die $!;
while (<FILE>)
{
 if ($_ =~/:(\d+)/ && $_ ne "\n")
 {
   chomp();
   push (@numbers,$1)
 }elsif ($_ =~/:(\w+)/ && $_ ne "\n")
 {
   chomp();
   push (@letters,$1)
 }
}

print "numbers:  ", @numbers, "\n";
print "letters:  ", @letters, "\n";

答案 1 :(得分:1)

修改了一些最佳实践和我自己的样式首选项(为可扩展性编程,因为我总是最终扩展代码,因此我尝试以通常可扩展的方式编程):

# Things we search for
my %patterns = (
    foo => qr/^matched pattern 1/,
    bar => qr/^matched pattern 2/,
    baz => qr/^matched pattern 3/,
);

# Where we store matches, initialized to empty array refs
my %matches = map { $_ => [] } keys %patterns;

open(my $fh, '<', $file) or die $!;
my %current_match;
LINE: while (my $line = <$fh>) {
    # We never want empty lines, so exit early
    next if $_ eq "\n";

    # Check current line for matches, to note which bucket we are saving into
    for my $matchable (keys %patterns) {
        # Skip to next unless it matches
        next unless $lines =~ $matches{$matchable};

        # Set the current match and jump to next line:
        $current_match = $matchable;
        next LINE;
    }

    # If there's a current match found, save the line
    push( @{$matches{$current_match}, $line ) if $current_match;
}

答案 2 :(得分:1)

你的计划应该做什么?您当前的程序正在寻找具有matched pattern并将 THERY VERY LINE 存储到三个不同阵列中的行。所有其他行都被忽略。

您展示了某种示例输出,但输出和输入之间没有真正的关系。

首先,了解references,因此您不需要五个不同的数组。在我的示例中,我使用数组数组来存储所有单独的文件。如果每个文件代表其他内容,则可以使用散列数组或数组散列或数组哈希散列来在统一结构中表示此数据。 (不要让我开始学习如何真正学习面向对象的Perl。首先了解一下参考文献。)

还可以获得有关现代Perl的书籍并学习新的Perl语法。看起来你的Perl引用是针对Perl 4.0的。 Perl 5.0自1994年以来就已经出现。在语法完成方式上,Perl 4和Perl 5之间存在很大差异。

use strict;
use warnings;

# Prints out your data strtucture
use Data::Dumper;

my $array_num;
my @array_of_arrays;

use constant  {
    PATTERN => qr/matched pattern/,
};

while (my $line = <DATA>) {
    chomp $line;
    next if $line =~ /^\s*$/;   #Skip blank lines
    if ($line =~ PATTERN) {
        if (not defined $array_num) {
            $array_num = 0;
        }
        else {
            $array_num++;
        }
        next;
    }
    push @{ $array_of_arrays[$array_num] }, $line;
}
print Dumper (\@array_of_arrays) . "\n";

__DATA__
matched pattern 1 
line1:10
line2:20
line3:30

next matched pattern 2
line1:5
line2:10
line3:15

next matched pattern 3
lineA:A
lineB:B
lineC:C

OUTPUT。每组线都在不同的数组中:

$VAR1 = [
      [
        'line1:10',
        'line2:20',
        'line3:30'
      ],
      [
        'line1:5',
        'line2:10',
        'line3:15'
      ],
      [
        'lineA:A',
        'lineB:B',
        'lineC:C'
      ]
    ];