我需要第二双这样的眼睛。 当条件(if或elsif)为真时,我会得到一个额外的换行符。 我不想那样。
use strict; use warnings; use autodie; use feature 'say'; my $filename = 'rr.txt'; open my $fh, '<', $filename; while (<$fh>) { my ($last_name, $first_name, $country) = split /[,\|]/, $_; my $middle_name; $country =~ s/^\(//; $country =~ s/\)$//; say "Last Name: $last_name"; say "First Name: $first_name"; say "Country: $country"; if ($first_name =~ /\w\s(\w+)/) { $middle_name = $1; say "Middle Name: $1"; } elsif ($first_name =~ /\w\-(\w+)/) { $middle_name = $1; say "Middle Name: $1"; } }该文件如下所示:
Reid, Matt|(AUS) Samper-Montana, Jordi|(ESP) Krajonovic, Filip|(SRB) Jones, Greg Luke|(AUS) Burquier, Gregoire|(FRA) Mandol, David|(ARG) Daniel Llosa, Miguel Horpo|(DOM)每当一个中间名称被输出时,它会在前面加一个换行符。 我不知道这是从哪里来的。 使用print和追加换行符也会发生同样的情况。
I need a second pair of eyes on this. I'm getting an extra newline whenever one of the conditionals (if or elsif) is true. I don't want that.
use strict; use warnings; use autodie; use feature 'say'; my $filename = 'rr.txt'; open my $fh, '<', $filename; while (<$fh>) { my ($last_name, $first_name, $country) = split /[,\|]/, $_; my $middle_name; $country =~ s/^\(//; $country =~ s/\)$//; say "Last Name: $last_name"; say "First Name: $first_name"; say "Country: $country"; if ($first_name =~ /\w\s(\w+)/) { $middle_name = $1; say "Middle Name: $1"; } elsif ($first_name =~ /\w\-(\w+)/) { $middle_name = $1; say "Middle Name: $1"; } }The file looks like this:
Reid, Matt|(AUS) Samper-Montana, Jordi|(ESP) Krajonovic, Filip|(SRB) Jones, Greg Luke|(AUS) Burquier, Gregoire|(FRA) Mandol, David|(ARG) Daniel Llosa, Miguel Horpo|(DOM)Whenever a middle name would get outputted it would be preceded by a newline. I don't know where that is coming from. The same thing happens by using print and appending a newline.
最满意答案
say在输出结尾添加换行符。 由于您没有chomp输入行,所以您阅读的国家/地区代码仍然附有换行符。 所以,你会得到双倍间隔的输出(来自国家之后的换行符,而不是中间名之前的换行符)。
修复:添加chomp; 在split线之前。
say adds a newline to the end of the output. The country codes you read have a newline still attached since you didn't chomp the input line. So, you get double-spaced output (from a newline after the country, rather than a newline before the middle name).
Fix: add chomp; before the split line.
更多推荐
发布评论