Arrays Perl。而循环行为会随着命令的添加而改变
下面的代码读取一个包含许多行的文件。文件的某些行包含四个元素。其他行仅包含第一个元素,后跟由制表符分隔的单个空格(它是一个制表符分隔的文件)。也就是说,有些行是“满”的,有些行是“空”的 这个脚本的目的是读取数据文件,找到一个空行的实例,然后记住前一行(一整行),滚动找到所有连续的空行,直到到达下一整行。这组行,连续的空白行,由紧接在前面的全行和紧接着的全行组成,将被一个子程序使用,该子程序将应用线性插值来“填充”空白行。插值步骤中将使用每组的侧翼全行中的信息。该脚本是对之前发布的问题的回答,由用户@Kenosis友好地提供。这里复制了它,但在布局上有一些很小的变化——不像@Kenosis最初提出的那样整洁。你可以在网站上看到这种互动Arrays Perl。而循环行为会随着命令的添加而改变,arrays,perl,for-loop,while-loop,Arrays,Perl,For Loop,While Loop,下面的代码读取一个包含许多行的文件。文件的某些行包含四个元素。其他行仅包含第一个元素,后跟由制表符分隔的单个空格(它是一个制表符分隔的文件)。也就是说,有些行是“满”的,有些行是“空”的 这个脚本的目的是读取数据文件,找到一个空行的实例,然后记住前一行(一整行),滚动找到所有连续的空行,直到到达下一整行。这组行,连续的空白行,由紧接在前面的全行和紧接着的全行组成,将被一个子程序使用,该子程序将应用线性插值来“填充”空白行。插值步骤中将使用每组的侧翼全行中的信息。该脚本是对之前发布的问题的回答,由
#/usr/bin/perl
严格使用;使用警告;
用法:[映射位置文件post SAS]\n\n“除非@ARGV==1;
my$mapfile=$ARGV[0];
打开(我的$FILE,“在push
行中你想做什么
它包括表达式$i++
,该表达式将$1
的值加1,因此该循环的每次迭代都将跳转到文件中的另一行
你的意思是说$i+1
?你真的要添加第二行代码来增加$i
?$i+=1
与$i+=2
不需要显式打开$ARGV[0]
。这就是
所做的。啊,我在递增,我不想。我只是试图将在循环中一次运行中打印的所有语句收集到一个数组中。这组行将转到用于线性插值的子例程。
#!/usr/bin/perl
use strict; use warnings;
die "usage: [ map positions file post SAS ]\n\n" unless @ARGV == 1;
my $mapfile = $ARGV[ 0 ];
open( my $FILE, "<$mapfile" );
my @file = <$FILE>;
for ( my $i = 1 ; $i < $#file ; $i++ ) # $#file returns the index of the last element in @file
{
if ( $file[$i] =~ /(?:\t\s){3}/ ) # if a blank line is found
{
print $file[ $i - 1 ]; # print preceding line
while ( $file[$i] =~ /(?:\t\s){3}/ and $i < $#file ) # keep printing so long as they are blank
# or end of file
{
#print $file[ $i++ ] # one-column, blank line
}
print $file[ $i ]; # print the succeeding full line
} # if
} # for
my @collect = (); # array collects a current set of consecutive lines needed for linear interpolation
my @file = <$FILE>;
for ( my $i = 1 ; $i < $#file ; $i++ ) # $#file returns the index of the last element in @file
{
if ( $file[$i] =~ /(?:\t\s){3}/ ) # if a blank line is found
{
print $file[ $i - 1 ]; # print preceding line
push( @collect, $file[ $i - 1 ] );
while ( $file[$i] =~ /(?:\t\s){3}/ and $i < $#file ) # keep printing so long as they are blank
# or end of file
{
#print $file[ $i++ ]; # one-column, blank line
push( @collect, $file[ $i++ ] )
}
print $file[ $i ]; # else, succeeding full line
push( @collect, $file[ $i ] );
} # if
} # for