Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Replacing whitespaces with a regex

by Dr Manhattan (Beadle)
on Mar 04, 2013 at 09:52 UTC ( #1021620=perlquestion: print w/ replies, xml ) Need Help??
Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I have an array containing a list of words. Some of the words contain a whitespace at the end, which is fine. I want to replace those elements with more than 1 whitespace at the end.

foreach my $words (@array) { if ($words =~ /(\s\s)$/) { #??? replace with just 1 \s } }

Or is there an easier way? Any help would be appreciated

Comment on Replacing whitespaces with a regex
Download Code
Re: Replacing whitespaces with a regex
by choroba (Abbot) on Mar 04, 2013 at 09:54 UTC
    Just do
    $words =~ s/\s\s+$/ /;
    whithout any if. If there is multiple whitespace, it will be replaced, if not, nothing happens.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Replacing whitespaces with a regex
by Enlil (Parson) on Mar 04, 2013 at 10:15 UTC
    for ( @array ) { s/\s\s+$/ / }
    or
    s/\s\s+$/ / for @array;
    since essentially you are testing all the words to see if they fit the pattern anyhow.
Re: Replacing whitespaces with a regex
by Anonymous Monk on Mar 04, 2013 at 10:43 UTC

    Small variation of the previous solutions, replace any spaces (even 1 space) with 1 space:

    for ( @array ) { s/\s+$/ / }
Re: Replacing whitespaces with a regex
by ggoebel (Sexton) on Mar 04, 2013 at 13:51 UTC

    When you have the time read perlre

    If you're in a rush... skip down to the section on "Quantifiers". TIMTOWTDI. Pick the way which you think you'll stand the best chance of understanding 3 weeks or 3 months from now when you're asked to tweak your code.

    Both:

    $work =~ s/\s+$/ /; # more than once

    and

    $work =~ s/\s{2,}$/ /; # at least twice

    should work.

      that should read "once or more" instead of "more than once"

      ++ anyway :-)

Re: Replacing whitespaces with a regex
by tmharish (Friar) on Mar 04, 2013 at 14:25 UTC

    The essence is that you are using regex to match - but what you want to do is use s/regex/replace/ to replace. Details on how to do it in your case has been covered extensively above.

Re: Replacing whitespaces with a regex
by ISAI student (Scribe) on Mar 04, 2013 at 16:24 UTC
    There is, of course, more than 1 to do it. My favorite is:
    map {s/\s+$/ /} @array;
    It uses the @array, which makes the code more readable, and PERL's great map function for 1 liners.
      map in a void context can be replaced by for:
      s/\s+$/ / for @array;
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Yes. But map brings me memories of the times I got to know the PERL magic, +and more than 1 way to do it. If I use for or foreach, I feel like I +use TCL/Python, and not enjoying an experience unique to PERL. map seems, to me, as more decidedly PERLISH that for. It's a personal thing, and this is why I have said that it is a person +al prefernce (unless there is a performance consideration).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1021620]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-07-30 05:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls