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
@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 に答える
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

あなたのプログラムは何をしているはずですか?現在のプログラムは、これらの非常に多くの行を3つの異なる配列にmatched pattern格納している行を探しています。他のすべての行は無視されます。

ある種の出力例を示していますが、出力と入力の間に実際の関係はありません。

まず、参照について学びます。したがって、5つの異なる配列は必要ありません。私の例では、配列の配列を使用して、すべての個別のファイルを格納しています。各ファイルが他の何かを表す場合は、ハッシュの配列、配列のハッシュ、または配列のハッシュのハッシュを使用して、このデータを統一された構造で表すことができます。(オブジェクト指向のPerlを実際にどのように学ぶべきかについて私に始めさせないでください。最初に参照のコツをつかんでください)。

また、最新のPerlに関する本を入手して、新しいPerl構文を学びましょう。PerlリファレンスはPerl4.0用のようです。Perl5.0は1994年からリリースされています。Perl4とPerl5の間には、構文の実行方法に大きな違いがあります。

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 に答える