Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Replacing multiple spaces into one character?

by r.joseph (Hermit)
on Oct 09, 2001 at 07:02 UTC ( #117623=perlquestion: print w/replies, xml ) Need Help??

r.joseph has asked for the wisdom of the Perl Monks concerning the following question:

Hey all. Suppose the string:  . . .Perlmonks  . . .is . .the . . . .coolest .place. Notice how each dot respresents a space (I did that for clarity, so that you could visualize multiple spaces (including leading spaces) - just realize that there can be any number of space between words).

I want to take the above string and turn it into: Perlmonks_is_the_coolest_place

Basically, replacing any number of spaces between words with ONE underscore. I tried:
$str =~ s/\s+/_/ig;
but that replaces each space with an underscore. Removing the "g" modifier from the regex does (obviously) no good.

I realize that this will probably end up being a very simple solution, but I have been studying all day and my brain is fried, so please help a poor, brain-fried college student out? Thanks all!

r. j o s e p h
"Violence is a last resort of the incompetent" - Salvor Hardin, Foundation by Issac Asimov

Replies are listed 'Best First'.
Re: Replacing multiple spaces into one character?
by chromatic (Archbishop) on Oct 09, 2001 at 07:10 UTC
    tr/ /_/s;, as /s is for squash multiple.

    Doing it in one line while removing leading whitespace is trickier. There are several potentially nasty regular expressions. Just say no.

    Update: Squashing several expressions onto one line technically counts, but it wasn't what I meant, smart guy!

      $str = ' say as simple as a b c :-) '; $str =~ s/^\s+|\s+$//g, $str =~ tr/ /_/s, print $str;
Re: Replacing multiple spaces into one character?
by John M. Dlugosz (Monsignor) on Oct 09, 2001 at 07:10 UTC
    Er, it works for me. To wit:
    $str= " perlmonks is the coolest"; $str =~ s/\s+/_/g; print $str;
    prints: _perlmonks_is_the_coolest

    (I left out the i modifier because space is the same in capital or lower-case. I don't think that would change the behavior, though!)


      Agreed, it works exactly as r.joseph presented it. However you may want to think about using
      $str =~ s/ +/_/g;
      instead, since it doesn't mess with your newline characters. That is, with your version "perl\n" becomes "perl_".

      - Boldra
Re: Replacing multiple spaces into one character?
by Zaxo (Archbishop) on Oct 09, 2001 at 07:36 UTC

    This uses the magical whitespace arg to split:

    my $str = " Perlmonks is the\tcoolest\n \tplace"; $str = join "_", split " ", $str; print $str,$/;
    The magic strips leading whitespace and is greedy about the rest. This is a little broader than you asked for, but still does what you want.

    After Compline,

Re: Replacing multiple spaces into one character?
by tachyon (Chancellor) on Oct 09, 2001 at 07:16 UTC

    Not recommended but TIMTOWTDI requires someone post this ;-)

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




      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


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




Re: Replacing multiple spaces into one character?
by pike (Monk) on Oct 09, 2001 at 11:22 UTC
    How about:

    $str = ' xx y z '; $str =~ s/(?<=\w)\s+(?=\w)/g; #replace only spaces between letters $str=~ tr/ //d; #delete leading and trailing spaces


Re: Replacing multiple spaces into one character?
by thunders (Priest) on Oct 09, 2001 at 19:01 UTC
    1 while $str =~ s/[ ]+/_/;
    will also work for this one, it's similar to the /g modifier except that it re-evaluates the entire string after every substitution. But this is for entertainment TIMTOWTDI value only cause something like this
    1 while $str =~ s/[ ]+/ _ /;
    will just keep running forever as it tries to replace the spaces it creates.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://117623]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-09-17 21:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found