Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 我想在文件中的字母后插入空格_String_Perl - Fatal编程技术网

String 我想在文件中的字母后插入空格

String 我想在文件中的字母后插入空格,string,perl,String,Perl,F1.txt tom1 a1 b1 c1 bob2 d2 e2 f2 a1 b1 c1 tom1 d2 e2 f2 bob2 a1b1c1tom1 d2e2f2bob2 结果 tom1 a1 b1 c1 bob2 d2 e2 f2 a1 b1 c1 tom1 d2 e2 f2 bob2 a1b1c1tom1 d2e2f2bob2 F2.txt tom1 a1 b1 c1 bob2 d2 e2 f2 a1 b1 c1 tom1 d2 e2 f2 bob2 a1b1c1tom1

F1.txt

tom1 a1 b1 c1
bob2 d2 e2 f2
a1 b1 c1 tom1
d2 e2 f2 bob2
a1b1c1tom1
d2e2f2bob2 
结果

tom1 a1 b1 c1
bob2 d2 e2 f2
a1 b1 c1 tom1
d2 e2 f2 bob2
a1b1c1tom1
d2e2f2bob2 
F2.txt

tom1 a1 b1 c1
bob2 d2 e2 f2
a1 b1 c1 tom1
d2 e2 f2 bob2
a1b1c1tom1
d2e2f2bob2 
大家好,有人能帮我解决这个问题吗。我的工作是将文件中每行的第一个字移到给定文件中该行的最后一个位置。如F2.txt所示。下面是我尝试过的代码,但没有得到所需的输出

use strict;
use warnings;

open FILE1, "<final.l";
open FILE2, ">>finala11.l";

my($line, @line);
while (<FILE1>) {
  $line=$_;
  chomp($line);
  @line = split("\t"," ",$line);

  push(@line, shift(@line));
  print FILE2 @line,"\n";
}

close (FILE1);
close (FILE2);
但预期输出如F2.txt所示。您能帮我找出代码中的错误以获得所需的结果吗?

您可以使用join:

print FILE2 join("\t", @line),"\n";
或:

使用严格;
使用警告;
打开文件1,“>finala11.l”;
我的($line,@line);
而(){
$line=$\;
chomp($line);

@line=split(“,$line,2);#将autosplit与数组切片相结合:

$ perl -F/\t/ -lane 'print join "\t", @F[1..$#F,0]' f1.txt > f2.txt

谢谢@cdtits。你帮了我很多。你的回答对我很有用。