Perl:Chomping 一个字符串后,它不打印字符串的值

Perl: After Chomping a string, it does not print the string's value

所以我目前正在尝试编写一个读取文件并写入另一个文件的 perl 脚本。目前,我遇到的问题是从已解析的 rows.I 提要中删除换行符,这样的文件

BetteDavisFilms.txt
1.
Wicked Stepmother (1989) as Miranda
A couple comes home from vacation to find that their grandfather has …
2.
Directed By William Wyler (1988) as Herself
During the Golden Age of Hollywood, William Wyler was one of the …
3.
Whales of August, The (1987) as Libby Strong
Drama revolving around five unusual elderly characters, two of whom …

最终我想把它变成这样的格式

1,Wicked Stepmother ,1989, as Miranda,A couple comes home from vacation to …
2,Directed By William Wyler ,1988, as Herself,During the Golden Age of …
3,"Whales of August, The ",1987, as Libby Strong,Drama revolving around five…

它成功地删除了对每个数字的识别,但后来我想删除\n然后替换“。”带有“,”。可悲的是,chomp 函数会破坏或隐藏数据,所以当我在 chomping $row 后打印时,没有任何显示......我应该怎么做才能纠正这个问题?

#!bin/usr/perl
use strict;
use warnings;

my $file = "BetteDavisFilms";
my @stack = ();

open (my $in , '<', "$file.txt" ) or die "Could not open to read \n ";
open (my $out , '>', "out.txt" ) or die "Could not out to  file  \n";

my @array = <$in>;

sub readandparse() {
    for(my $i = 0 ; $i < scalar(@array); $i++) {
        my $row = $array[$i];

        if($row =~ m/\d[.]/) {
            parseFirstRow($row);
        }
    }
}

sub parseFirstRow() {
    my $rowOne = shift;
    print $rowOne; ####prints a number
    chomp($rowOne);
    print $rowOne; ###prints nothing
    #$rowOne =~ s/./,/;
}

#call to run program
readandparse();

您的行以 CR LF 结尾。您移除 LF,留下 CR。您的终端正在将光标归位到 CR 上,导致下一行输出覆盖最后一行输出。

$ perl -e'
   print "XXXXXX\r";
   print "xxx\n";
'
xxxXXX

修复您的输入文件

dos2unix file

或者删除 CR 和 LF。

s/\s+\z//   # Instead of chomp