有幾個選項右修剪:
1.更換CR所有出現的/ LF與LF:$output =~ s/\r\n/\n/g; #instead of \r\n might want to use \012\015
2.刪除所有尾隨空白:output =~ s/\s+$//g;
3.啜食和拆分:
#!/usr/bin/perl -w
use strict;
use LWP::Simple;
sub main{
createfile();
outputfile();
}
main();
sub createfile{
(my $file = $0)=~ s/\.pl/\.txt/;
open my $fh, ">", $file;
print $fh "1\n2\r\n3\n4\r\n5";
close $fh;
}
sub outputfile{
(my $filei = $0)=~ s/\.pl/\.txt/;
(my $fileo = $0)=~ s/\.pl/out\.txt/;
open my $fin, "<", $filei;
local $/; # slurp the file
my $text = <$fin>; # store the text
my @text = split(/(?:\r\n|\n)/, $text); # split on dos or unix newlines
close $fin;
local $" = ", "; # change array scalar separator
open my $fout, ">", $fileo;
print $fout "@text"; # should output numbers separated by comma space
close $fout;
}
Rubberduck效應。它永遠不會失敗! :) – 2010-07-02 15:30:27
請記住,這是去除人物'的所有實例\ r'和'\ N',而不是字符串'\ r \ N'(只是櫃面'\ r'或'\ N'可能是有效的值你需要在其他地方) – 2010-07-02 20:37:25