0

我在我的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
4

3 回答 3

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;
}
于 2012-10-24T00:51:36.193 回答
1

你的程序应该做什么?您当前的程序正在寻找具有这些行的行并将这些matched pattern存储到三个不同的数组中。所有其他行都被忽略。

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

首先,了解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

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

$VAR1 = [
      [
        'line1:10',
        'line2:20',
        'line3:30'
      ],
      [
        'line1:5',
        'line2:10',
        'line3:15'
      ],
      [
        'lineA:A',
        'lineB:B',
        'lineC:C'
      ]
    ];
于 2012-10-24T00:59:31.713 回答
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";
于 2012-10-24T00:13:09.117 回答