Regex 如何在输出中保存格式为FIRSTNAME LASTNAME和output is LASTNAME、FIRSTNAME的产品输出
我的输出没有保存到hi.txt中Regex 如何在输出中保存格式为FIRSTNAME LASTNAME和output is LASTNAME、FIRSTNAME的产品输出,regex,perl,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; } open(my$fh,'>','C:\Users\yukari\Desktop\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;
}
open(my$fh,'>','C:\Users\yukari\Desktop\hi.txt');
$x=;
如果($x=~/(.+)(.+)/)
{
打印“$2”;
打印“$1\n”;
打印$fh=“$2,$1\n”;
收盘价$fh;
}
你能回忆起编码吗?有几个问题你可以很好地解决
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;
}
open(my$fh,“>”,“hi.txt”);
$x=;
#1.无论何时从用户处获取值,都应该执行chomp
chomp(x美元);
如果($x=~/(.+)(.+)/)
{
打印“$2”;
打印“$1\n”;
#2.无需将变量存储在FILEHANDLE中
打印$fh“$2,$1\n”;
收盘价$fh;
}
谢谢。我会这样说以防万一:
+
是贪婪的,第一个会消耗整个字符串直到最后一个空格。这是故意的吗?我已经在代码中添加了缩进。不客气,但请以后自己做。为什么只有在正则表达式匹配时才关闭文件?