简体   繁体   English

从perl文件中仅打印一行中由制表符分隔的十个单词

[英]to print only ten words separated by tab in a line from a file in perl

#! /bin/usr/perl
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;

I want to have only ten words or (number also counted as a word) but what I am getting is only first the ten words are printed rest of the file is blank. 我只想有十个单词,或者(数字也算作一个单词),但是我得到的只是第一个单词,剩下的十个单词是空白。

Desired output: 所需的输出:

1 my name is abcde fg 1我叫abcde fg

your name is micheal and you study in class 5 你的名字叫米切尔(Micheal),你在5年级学习

The initial problem with your code is syntax errors. 代码的最初问题是语法错误。 The second problem is that you have not told Perl to help you write better code. 第二个问题是您没有告诉Perl帮助您编写更好的代码。 Almost all Perl code should have use strict; use warnings; 几乎所有的Perl代码都应use strict; use warnings;代码use strict; use warnings; use strict; use warnings; , so your code should start ,因此您的代码应该开始

#! /bin/usr/perl
use strict;
use warnings;

The use strict; use strict; will mean you must declare all the variables used, but that is a good thing. 这将意味着您必须声明所有使用的变量,但这是一件好事。 The two lines will also point out the syntax errors. 这两行还将指出语法错误。

The only Perl programs that should not have use strict; 唯一不应该use strict; Perl程序use strict; and use warnings; use warnings; are those where their author knows exactly why are being omitted and can explain the reasons to other people. 是他们的作者确切知道为什么被省略并可以向其他人解释原因的地方。

perl -wnlaF'\t' -E 'say join "\t", @F[0 .. (9 <= $#F ? 9 : $#F)]' input.txt > output.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM