对于另一段代码,如何在while循环(perl)中保留前一行文本中的变量?
我是Perl新手,无法找到这个特定问题的答案。我正在分析一些文本。我想从一行中的一些条目被用作其他行的输入。在下文中,我希望$sec用于以“M”开头的邮件。 我的代码如下:对于另一段代码,如何在while循环(perl)中保留前一行文本中的变量?,perl,text,Perl,Text,我是Perl新手,无法找到这个特定问题的答案。我正在分析一些文本。我想从一行中的一些条目被用作其他行的输入。在下文中,我希望$sec用于以“M”开头的邮件。 我的代码如下: #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 = 6666
#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";
}
在循环之前和外部声明
$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.
my$sec;
#循环-我猜这是一个while循环,在文件中的行上迭代。
而(){
my$message=substr$\u0,1;
如果($message eq“T”){
#在此处分配给$sec
}
如果($message eq“M”){
#在这里使用$sec
}
}#循环结束。
这里有很多假设:如果在
T
之后有多个M
s,它们都使用相同的$sec
值,等等。感谢您的快速响应。我会试试这个。