使用'说'的奇怪输出(Strange output from using 'say')

编程入门 行业动态 更新时间:2024-10-28 05:27:00
使用'说'的奇怪输出(Strange output from using 'say')

我需要第二双这样的眼睛。 当条件(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.

更多推荐

本文发布于:2023-08-03 13:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1392038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   Strange   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!