In Perl, how can I convert all newlines to spaces in a string?

15,226

Solution 1

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

For your question, tr/// is more appropriate than 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:

#!/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 ;-)

Solution 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.

Solution 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);

Solution 4

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

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

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;
Share:
15,226
Prasad
Author by

Prasad

I am back

Updated on June 05, 2022

Comments

  • Prasad
    Prasad almost 2 years

    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"
    
  • Prasad
    Prasad almost 15 years
    is the substitution is faster than other functions ?
  • Prasad
    Prasad almost 15 years
    which one is faster substitution or split&join ?
  • ghostdog74
    ghostdog74 almost 15 years
    i haven't time it, but you can try for yourself using Benchmark module
  • friedo
    friedo almost 15 years
    Why don't you try it and find out?
  • Prasad
    Prasad almost 15 years
    for my operation substitution is faster
  • Prasad
    Prasad almost 15 years
    for my string substitution is faster
  • Telemachus
    Telemachus almost 15 years
    It's a good idea to avoid $a and $b outside of sort. Since the OP might not know why those variables are special, I added a comment to his original question saying why (briefly).