2

私は Perl を初めて使用し、この特定の問題に対する答えを見つけることができませんでした。私はいくつかのテキストを解析しています。行のエントリの一部を他の行の入力として使用したいと考えています。以下では、「M」で始まるメッセージに $sec を使用したいと考えています。私のコードは次のとおりです。

#identify the type of message here:
my $message = substr $_, 0, 1;

if ($message eq "T") {

    my $sec = substr $_, 1, 5;

    #no ms entry here
    my $ms = 66666;

    push @add_orders, $_;
    print add_order_file "$sec, $ms\n";  
}

if ($message eq "M") {

    my $ms=substr $_, 1, 3;
    push @add_orders, $_;

    #I want $sec to be from the previous 
    print add_order_file "$sec, $ms \n";
}
4

1 に答える 1

2

ループの前と外側で変数を宣言する$secと、反復間で値が保持されます。

my $sec;

# The loop - I've guessed it's a while loop iterating over lines in a file.
while ( <> ) {

    my $message = substr $_,0,1;

    if ( $message eq "T" ) {
        # Assign to $sec here
    }
    if ( $message eq "M" ) {
        # Use $sec here
    }

} # End of the loop.

ここでは多くの仮定が行われています:Mの後に複数の がある場合、Tそれらはすべて同じ$sec値を使用するなどです。

于 2012-09-03T22:40:26.950 に答える