replace multiple spaces between words with single space.

Discussion in 'Perl Misc' started by Shashank Khanvilkar, Mar 14, 2005.

  1. Hi,
    Suppose I have a variable defined as:

    $a = "This is a good example";

    Note that there are multiple blanks between the words.
    If i want to convert the above string to
    $b = "This is a good example"

    Is there any shortcut to do the above.
    I know the long way as

    @a_T = split(/\s/, $a);
    foreac $i (@a_T){
    if ($i){
    $b = $b.$i." ";
    }
    }

    I want to use substitute but canot seem to get it right..
    Thanks for any help
    Shashank
    Shashank Khanvilkar, Mar 14, 2005
    #1
    1. Advertising

  2. In article <d14iaq$d4i$>,
    Shashank Khanvilkar <> wrote:
    :Suppose I have a variable defined as:

    :$a = "This is a good example";

    :Note that there are multiple blanks between the words.
    :If i want to convert the above string to
    :$b = "This is a good example"

    :Is there any shortcut to do the above.

    ($b = $a) =~ s/ +/ /g;

    Or if you want to generalize this a bit,

    ($b = $a) =~ s/\s+/ /g;

    \s matches whitespace (includes tabs and various non-ASCII whitespace.)
    The + modifier means "one or more of the previous group".
    The trailing g means "global replace" -- that is, do for all occurances
    in the input string.

    The other trick is the ($b = $a). If you were to use

    $b = $a =~ s/\s+/ /g;

    then $b would be assigned the result of the =~ operation; in a scalar
    context, =~ with a substitution returns the number of substitutions made,
    so $b would end up as a count instead of as the new string. The
    ($b = $a) part causes the assignment to be done first, and then the =~
    acts upon the string so created.

    --
    Warning: potentially contains traces of nuts.
    Walter Roberson, Mar 14, 2005
    #2
    1. Advertising

  3. Shashank Khanvilkar <> wrote in news:d14iaq$d4i$1
    @newsx.cc.uic.edu:

    > Suppose I have a variable defined as:


    Let's suppose.

    > $a = "This is a good example";


    It is probably not a good idea to use $a and $b. See perldoc -f sort.

    > Note that there are multiple blanks between the words.
    > If i want to convert the above string to
    > $b = "This is a good example"


    Ditto.

    > I know the long way as


    use strict;
    use warnings;

    > @a_T = split(/\s/, $a);
    > foreac $i (@a_T){
    > if ($i){
    > $b = $b.$i." ";
    > }
    > }


    Please post real code.

    syntax error at v.pl line 2, near "){"
    Execution of v.pl aborted due to compilation errors.

    Have you read the relevant section in perldoc perlop?

    use strict;
    use warnings;

    my $src = q{This is a good example};

    my $dest;

    $dest = join ' ', split /\s+/, $src;
    print "$dest\n";

    ($dest = $src) =~ s/\s+/ /g;
    print "$dest\n";

    Sinan
    A. Sinan Unur, Mar 14, 2005
    #3
  4. Shashank Khanvilkar wrote:
    > Suppose I have a variable defined as:
    >
    > $a = "This is a good example";
    >
    > Note that there are multiple blanks between the words.
    > If i want to convert the above string to
    > $b = "This is a good example"
    >
    > Is there any shortcut to do the above.


    ( $b = $a ) =~ y/ //s;


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Mar 14, 2005
    #4
  5. Shashank Khanvilkar

    Anno Siegel Guest

    Walter Roberson <-cnrc.gc.ca> wrote in comp.lang.perl.misc:
    > In article <d14iaq$d4i$>,
    > Shashank Khanvilkar <> wrote:
    > :Suppose I have a variable defined as:
    >
    > :$a = "This is a good example";
    >
    > :Note that there are multiple blanks between the words.
    > :If i want to convert the above string to
    > :$b = "This is a good example"
    >
    > :Is there any shortcut to do the above.
    >
    > ($b = $a) =~ s/ +/ /g;


    tr/// does that a bit faster:

    ($b = $a) =~ tr/ //s;

    Anno
    Anno Siegel, Mar 15, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BerlinBrown
    Replies:
    6
    Views:
    4,475
  2. Lasse Edsvik

    replace words with bold words

    Lasse Edsvik, Oct 5, 2003, in forum: ASP General
    Replies:
    9
    Views:
    234
  3. Michelle Pace
    Replies:
    9
    Views:
    130
    Joel VanderWerf
    Apr 26, 2011
  4. Francesco Moi
    Replies:
    3
    Views:
    89
    Tad McClellan
    Aug 20, 2003
  5. Lei
    Replies:
    9
    Views:
    105
    Jim Keenan
    Nov 26, 2004
Loading...

Share This Page