Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Replacing multiple spaces into one character?

by tachyon (Chancellor)
on Oct 09, 2001 at 07:16 UTC ( [id://117630] : note . print w/replies, xml ) Need Help??


in reply to Replacing multiple spaces into one character?

Not recommended but TIMTOWTDI requires someone post this ;-)

$str =' Perlmonks is a very cool place '; $str = join '_', split /\s+/, $str; print $str;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Replacing multiple spaces into one character?
by blakem (Monsignor) on Oct 09, 2001 at 07:36 UTC
    you could also use the magical ' ' (single space) arg to split, which will remove leading spaces as well...
    #!/usr/bin/perl -wT use strict; my $str =' Perlmonks is a very cool place '; $str = join '_', split ' ', $str; print $str, "\n"; =OUTPUT Perlmonks_is_a_very_cool_place

    -Blake

      Thanks blakem that null string that you get using /\s+/is a pain. I never realised that ' ' was magical.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Yeah, its a little screwy. ' ' is magical, but / / is not. I admit to checking the docs on occasion to remember which one is which.

        -Blake