Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > replace multiple spaces between words with single space.

Reply
Thread Tools

replace multiple spaces between words with single space.

 
 
Shashank Khanvilkar
Guest
Posts: n/a
 
      03-14-2005
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
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      03-14-2005
In article <d14iaq$d4i$(E-Mail Removed)>,
Shashank Khanvilkar <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-14-2005
Shashank Khanvilkar <(E-Mail Removed)> 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      03-14-2005
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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      03-15-2005
Walter Roberson <(E-Mail Removed)-cnrc.gc.ca> wrote in comp.lang.perl.misc:
> In article <d14iaq$d4i$(E-Mail Removed)>,
> Shashank Khanvilkar <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman ASP .Net 3 04-14-2007 07:16 AM
removing spaces between single characters with regual expression ciapecki Ruby 0 09-12-2006 05:50 PM
Help: separate difference length of spaces between words Lei Perl Misc 9 11-26-2004 03:23 PM
replace words with bold words Lasse Edsvik ASP General 9 10-07-2003 01:19 PM



Advertisments