从perl中输出文件的每行中有五个单词的输入文件中打印para

时间:2014-04-01 10:04:47

标签: perl

#! /bin/usr/perl
use strict ;
use warning;
print "Enter your database name: \n";

chomp($db=<STDIN>);
open(R1,"$db")||die("error");
 while($line1=(<R1>))
{
$l1=$line1;
@arr1= split("  ",$l1);
         for $i=(0..9)
           {
              print  "@arr1[$i] \t";
              print join (@arr1), "\n";
           }
}
close(R1);
exit;

从perl

输出文件中每行五个单词的输入文件中打印para

输入:

我的名字是k s和我 在第7课学习。我12岁 一岁,我喜欢到世界各地旅行。我喜欢泰国菜。

理想的输出:
我叫k s 在第7课。我
我12岁和 我喜欢旅行 全世界。我喜欢 泰国菜

你能帮忙解决打印功能

1 个答案:

答案 0 :(得分:2)

可能使用正则表达式:

use strict;
use warnings;

while (<DATA>) { chomp;
    print "$1\n" while /((?:\w+\W*){1,5})/g;
}

__DATA__
My name is k s and i study in class 7. i am 12 year old and i like to
travel all over the world.i like thai food

给出:

My name is k s 
and i study in class 
7. i am 12 year 
old and i like to
travel all over the world.
i like thai food