如何在输出中保存生成输出,格式为FIRSTNAME LASTNAME,输出为LASTNAME,FIRSTNAME

时间:2017-10-06 01:49:33

标签: regex perl

我的输出不保存到hi.txt

open(my $fh, '>', 'C:\Users\yukari\Desktop\hi.txt');

$x = <STDIN>;
if ($x =~ /(.+) (.+)/) 
{
  print " $2 ,";
  print " $1\n";

  print $fh = "$2, $1 \n" ;
  close $fh;
}

1 个答案:

答案 0 :(得分:-2)

你能回忆一下编码吗,你可以很好地逮捕几个问题

open(my $fh, '>', "hi.txt");
$x = <STDIN>;

#1. You should do the chomp whenever getting the values from user
chomp($x);

if ($x =~ /(.+) (.+)/) 
{
    print " $2 ,";
    print " $1\n";

    #2. No need to store the variable in the FILEHANDLE
    print $fh "$2, $1 \n" ;
    close $fh;
}

感谢。