Perl 打印每个句子的第三个单词 使用警告; $reff_filepath=“/home/sharma/Documents/ref.txt”; 打开(CONFIG),

Perl 打印每个句子的第三个单词 使用警告; $reff_filepath=“/home/sharma/Documents/ref.txt”; 打开(CONFIG),,perl,Perl,在循环的每次迭代中打印数组 您需要将“print”移到循环之外,并向数组的每个元素添加“\n”: use warnings; $reff_filepath="/home/sharma/Documents/ref.txt"; open (CONFIG,"<","$reff_filepath")or die "Unable to open $reff_filepath: $!"; while(<CONFIG>) { chomp; @cols = (split(/

在循环的每次迭代中打印数组

您需要将“print”移到循环之外,并向数组的每个元素添加“\n”:

use warnings;

$reff_filepath="/home/sharma/Documents/ref.txt";
open (CONFIG,"<","$reff_filepath")or die "Unable to open $reff_filepath: $!";

while(<CONFIG>) {
    chomp;
    @cols = (split(/ /))[2];
    push(@array, @cols);
    print "@array\n";
}

close CONFIG;

如果要打印第三个单词,只需使用

push(@array, @cols);

}
print "$_\n" foreach @arr;

将输入分隔符定义为“.”如何

$/=。;
while(){
$i=0;
@单词=拆分(/\s+/);

而($i一行解决方案,从每行打印出(非句子)每三个单词,它不会打印出少于3个单词的空行:

perl-e'@a=map{(split/\s+/)[2]};$“=”\n;print“@a\n””

此解决方案将打印少于3个单词的空行:

perl-e'print map{(split/\s+/)[2]。“\n”};

@cols = grep {not ++$i % 3} (split /\s+/)
$/ = .;

while(<>) {
  $i = 0;
  @words = split(/\s+/);
  while ($i <= $#words) {
     push (@array, @words[$i]);
     $i+=3;
  }
}
print "@array\n";