![](/img/trans.png)
[英]Why does 'chomp' fail to remove newlines on Windows XP with Eclipse and Cygwin Perl?
[英]Perl chomp does not remove all the newlines
我的代碼如下:
#!/usr/bin/perl
use strict;
use warnings;
open(IO,"<source.html");
my $variable = do {local $/; <IO>};
chomp($variable);
print $variable;
但是,當我打印它時,它仍然有換行符?
它刪除了最后一個換行符。
由於你在整個文件中啜飲,你將不得不做一個正則表達式替換來擺脫它們:
$variable =~ s/\n//g;
Chomp只從字符串的末尾刪除換行符(實際上是$/
的當前值,但這是你的情況下的換行符)。 要刪除所有換行,請執行以下操作:
$variable =~ y/\n//d;
或者你可以chomp
每一行,你在讀它:
#!/usr/bin/perl
use strict;
use warnings;
open my $io, '<', 'source.html';
my $chomped_text = join '', map {chomp(my $line = $_); $line} <$io>;
print $chomped_text;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.