在Perl中,如何将所有换行符转换为字符串中的空格?

时间:2022-03-06 21:04:20

Are there any functions are available for converting all newlines in a string to spaces?

是否有任何函数可用于将字符串中的所有换行符转换为空格?

For example:

$a = "dflsdgjsdg
dsfsd 
gf 
sgd 
g  
sdg
sdf
gsd";

The result is am looking for is:

我要找的结果是:

$a = "dflsdgjsdg dsfsd gf sgd g sdg sdf gsd"

4 个解决方案

#1


I would recommend restricting the use of $a and $b to sort routines only.

我建议限制使用$ a和$ b来排序例程。

For your question, tr/// is more appropriate than s///:

对于你的问题,tr ///比s ///更合适:

#!/usr/bin/perl

use strict;
use warnings;

my $x = q{dflsdgjsdg
dsfsd
gf
sgd
g
sdg
sdf
gsd};

$x =~ tr{\n}{ };

print $x, "\n";
__END__

Output:

C:\Temp> ttt
dflsdgjsdg dsfsd gf sgd g sdg sdf gsd

Update: I do not think TMTOWTDI justifies using anything other than tr/// here. First, semantically, what the OP is asking for is transliteration and therefore it makes sense to use transliteration. Second, at least on my Windows XP laptop with 5.10, the benchmark module provides a clear contrast:

更新:我不认为TMTOWTDI在这里使用除了tr ///以外的任何东西是合理的。首先,在语义上,OP要求的是音译,因此使用音译是有意义的。其次,至少在我的带有5.10的Windows XP笔记本电脑上,基准模块提供了明显的对比:

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark qw( cmpthese );

use constant LONG_STRING => "\n" x 1_000_000;

cmpthese -10, {
    subst => sub {
        my $x = LONG_STRING;
        $x =~ s{\n}{ }g;
        return;
    },
    split_join => sub {
        my $x = LONG_STRING;
        $x = join ' ', split /\n/, $x;
        return;
    },
    tr => sub {
        my $x = LONG_STRING;
        $x =~ tr{\n}{ };
        return;
    },
    nop => sub {
        my $x = LONG_STRING;
        return;
    }
};
__END__

Results:

              Rate split_join      subst         tr        nop
split_join 0.354/s         --       -85%      -100%      -100%
subst       2.40/s       578%         --       -99%      -100%
tr           250/s     70514%     10320%         --       -92%
nop         3025/s    854076%    125942%      1110%         --

One more update: I should point out that the relative performance of tr/// to s/// depends on the size and composition of the source string. The case I chose for illustration here is definitely extreme. Using less extreme input strings, the performance ratio seems to be closer to 15:1 rather than 100:1 ;-)

还有一个更新:我应该指出tr ///到s ///的相对性能取决于源字符串的大小和组成。我在这里选择插图的情况绝对是极端的。使用不太极端的输入字符串,性能比似乎接近15:1而不是100:1 ;-)

#2


Try the following program:

尝试以下程序:

#!/usr/bin/perl

use strict;
use warnings;

my $a = 'dflsdgjsdg
dsfsd
gf
sgd
g
sdg
sdf
gsd';
$a =~ s{\n}{ }g;

print $a;

The program simply uses a regular expression to search for newlines and replace them with spaces globally.

该程序只是使用正则表达式来搜索换行符并用全局空格替换它们。

#3


how about substitition

如何替代

$a = "dflsdgjsdg
dsfsd 
gf 
sgd 
g  
sdg
sdf
gsd";

$a =~ s/\n/ /g;
print $a;

or using split and join

或使用拆分和连接

@s =split /\n/,$a;
print join(" ",@s);

#4


This is a nice question because it embodies Perl's TMTOWTDI.

这是一个很好的问题,因为它体现了Perl的TMTOWTDI。

The answers above give 3 options, all of which are valid. I'll summarize them here.

上面的答案给出了3个选项,所有选项都是有效的。我在这里总结一下。

The string is:

字符串是:

$a = "dflsdgjsdg
dsfsd 
gf 
sgd 
g  
sdg
sdf
gsd";

substitution

$a =~ s/\n/ /g;

transliteration

$a =~ tr/\n/ /;

split/join

$a = join " ", split "\n", $a;

#1


I would recommend restricting the use of $a and $b to sort routines only.

我建议限制使用$ a和$ b来排序例程。

For your question, tr/// is more appropriate than s///:

对于你的问题,tr ///比s ///更合适:

#!/usr/bin/perl

use strict;
use warnings;

my $x = q{dflsdgjsdg
dsfsd
gf
sgd
g
sdg
sdf
gsd};

$x =~ tr{\n}{ };

print $x, "\n";
__END__

Output:

C:\Temp> ttt
dflsdgjsdg dsfsd gf sgd g sdg sdf gsd

Update: I do not think TMTOWTDI justifies using anything other than tr/// here. First, semantically, what the OP is asking for is transliteration and therefore it makes sense to use transliteration. Second, at least on my Windows XP laptop with 5.10, the benchmark module provides a clear contrast:

更新:我不认为TMTOWTDI在这里使用除了tr ///以外的任何东西是合理的。首先,在语义上,OP要求的是音译,因此使用音译是有意义的。其次,至少在我的带有5.10的Windows XP笔记本电脑上,基准模块提供了明显的对比:

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark qw( cmpthese );

use constant LONG_STRING => "\n" x 1_000_000;

cmpthese -10, {
    subst => sub {
        my $x = LONG_STRING;
        $x =~ s{\n}{ }g;
        return;
    },
    split_join => sub {
        my $x = LONG_STRING;
        $x = join ' ', split /\n/, $x;
        return;
    },
    tr => sub {
        my $x = LONG_STRING;
        $x =~ tr{\n}{ };
        return;
    },
    nop => sub {
        my $x = LONG_STRING;
        return;
    }
};
__END__

Results:

              Rate split_join      subst         tr        nop
split_join 0.354/s         --       -85%      -100%      -100%
subst       2.40/s       578%         --       -99%      -100%
tr           250/s     70514%     10320%         --       -92%
nop         3025/s    854076%    125942%      1110%         --

One more update: I should point out that the relative performance of tr/// to s/// depends on the size and composition of the source string. The case I chose for illustration here is definitely extreme. Using less extreme input strings, the performance ratio seems to be closer to 15:1 rather than 100:1 ;-)

还有一个更新:我应该指出tr ///到s ///的相对性能取决于源字符串的大小和组成。我在这里选择插图的情况绝对是极端的。使用不太极端的输入字符串,性能比似乎接近15:1而不是100:1 ;-)

#2


Try the following program:

尝试以下程序:

#!/usr/bin/perl

use strict;
use warnings;

my $a = 'dflsdgjsdg
dsfsd
gf
sgd
g
sdg
sdf
gsd';
$a =~ s{\n}{ }g;

print $a;

The program simply uses a regular expression to search for newlines and replace them with spaces globally.

该程序只是使用正则表达式来搜索换行符并用全局空格替换它们。

#3


how about substitition

如何替代

$a = "dflsdgjsdg
dsfsd 
gf 
sgd 
g  
sdg
sdf
gsd";

$a =~ s/\n/ /g;
print $a;

or using split and join

或使用拆分和连接

@s =split /\n/,$a;
print join(" ",@s);

#4


This is a nice question because it embodies Perl's TMTOWTDI.

这是一个很好的问题,因为它体现了Perl的TMTOWTDI。

The answers above give 3 options, all of which are valid. I'll summarize them here.

上面的答案给出了3个选项,所有选项都是有效的。我在这里总结一下。

The string is:

字符串是:

$a = "dflsdgjsdg
dsfsd 
gf 
sgd 
g  
sdg
sdf
gsd";

substitution

$a =~ s/\n/ /g;

transliteration

$a =~ tr/\n/ /;

split/join

$a = join " ", split "\n", $a;