Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbie question - how to replace multiple whitespace within astring?

Reply
Thread Tools

Newbie question - how to replace multiple whitespace within astring?

 
 
Brian Tully
Guest
Posts: n/a
 
      06-22-2004
Sorry if this is too basic a question but I just inherited a handful of Ruby
code and I'm looking to do something that is unbelievably simple to do with
PHP or Perl, but I'm having no luck figuring out how to do it in Ruby (no
thanks to "Ruby In A Nutshell").

In the middle of a Ruby script I have a loop with the variable:

@user['state']

Which is populated by data from an external text file. However I'm noticing
that the text file is not consistent in how it reports the user's State and
will occasionally add an extra whitespace between States comprised of 2
words, e.g., "New York".

In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
Perl I would do something like $state =~ s/\s\s/\s/$state;

How would one accomplish this in Ruby?

Thanks in advance!

Regards,
Brian




 
Reply With Quote
 
 
 
 
Andreas Schwarz
Guest
Posts: n/a
 
      06-22-2004
Brian Tully wrote:
> In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
> Perl I would do something like $state =~ s/\s\s/\s/$state;
>
> How would one accomplish this in Ruby?


"New York".gsub(/ /, ' ')
=> "New York"

- or better -

"New \t York".gsub(/\s+/, ' ')
=> "New York"
 
Reply With Quote
 
 
 
 
Jamey Cribbs
Guest
Posts: n/a
 
      06-22-2004
Brian Tully wrote:

>In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
>Perl I would do something like $state =~ s/\s\s/\s/$state;
>
>How would one accomplish this in Ruby?
>
>

state.squeeze!(" ")

Now, how easy is that?!

Jamey

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.


 
Reply With Quote
 
Brian Tully
Guest
Posts: n/a
 
      06-22-2004
on 6/22/04 3:01 PM, Jamey Cribbs at http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Brian Tully wrote:
>
>> In PHP I would simply perform an eregi_replace(" ", " ", $state); and in
>> Perl I would do something like $state =~ s/\s\s/\s/$state;
>>
>> How would one accomplish this in Ruby?
>>
>>

> state.squeeze!(" ")
>
> Now, how easy is that?!


Simply MAH-velous! thanks a bunch!

Just to clarify...

Should I use state.squeeze(" ") to ensure that it always returns the state
even if nothing was changed? squeeze! seems to return nil if nothing needs
to be changed.

Thanks again!
Brian




 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      06-22-2004
Brian Tully wrote:

> Should I use state.squeeze(" ") to ensure that it always returns the state
> even if nothing was changed? squeeze! seems to return nil if nothing needs
> to be changed.


str.squeeze! is destructive (changes the String object referred to by
str), and str.squeeze is not. So it just depends on whether you need to
keep the original string intact (I'd guess not).


 
Reply With Quote
 
Brian Tully
Guest
Posts: n/a
 
      06-22-2004
on 6/22/04 3:36 PM, Joel VanderWerf at (E-Mail Removed) wrote:

> Brian Tully wrote:
>
>> Should I use state.squeeze(" ") to ensure that it always returns the state
>> even if nothing was changed? squeeze! seems to return nil if nothing needs
>> to be changed.

>
> str.squeeze! is destructive (changes the String object referred to by
> str), and str.squeeze is not. So it just depends on whether you need to
> keep the original string intact (I'd guess not).



Hmmm I'm a little hazy...

I want Ruby to strip out any instances of multiple whitespace and return the
"correct" string. I also want it to return the string regardless of whether
the squeeze function modified it.

So in these examples I would hope for the following:

@state = @user['state'].squeeze(" ")

if @user['state'] was originally "New York" I would hope that the above
would set @state to "New York".

In addition if @user['state'] was originally "California" I would hope that
the above would still set @state to "California".



squeeze! - Squeezes str in place, returning either str, or nil if no
changes were made.

I interpret this as if no changes were made "nil" will be returned, so in my
example of "California" wouldn't this set @state to nil?





 
Reply With Quote
 
Jamey Cribbs
Guest
Posts: n/a
 
      06-22-2004
Brian Tully wrote:

>So in these examples I would hope for the following:
>
> @state = @user['state'].squeeze(" ")
>
>if @user['state'] was originally "New York" I would hope that the above
>would set @state to "New York".
>
>In addition if @user['state'] was originally "California" I would hope that
>the above would still set @state to "California".
>
>
>
>squeeze! - Squeezes str in place, returning either str, or nil if no
>changes were made.
>
>I interpret this as if no changes were made "nil" will be returned, so in my
>example of "California" wouldn't this set @state to nil?
>
>
>

You are correct.

Jamey

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.


 
Reply With Quote
 
Mark Sparshatt
Guest
Posts: n/a
 
      06-22-2004
Brian Tully wrote:

>on 6/22/04 3:36 PM, Joel VanderWerf at (E-Mail Removed) wrote:
>
>
>
>>Brian Tully wrote:
>>
>>
>>
>>>Should I use state.squeeze(" ") to ensure that it always returns the state
>>>even if nothing was changed? squeeze! seems to return nil if nothing needs
>>>to be changed.
>>>
>>>

>>str.squeeze! is destructive (changes the String object referred to by
>>str), and str.squeeze is not. So it just depends on whether you need to
>>keep the original string intact (I'd guess not).
>>
>>

>
>
>Hmmm I'm a little hazy...
>
>I want Ruby to strip out any instances of multiple whitespace and return the
>"correct" string. I also want it to return the string regardless of whether
>the squeeze function modified it.
>
>So in these examples I would hope for the following:
>
> @state = @user['state'].squeeze(" ")
>
>if @user['state'] was originally "New York" I would hope that the above
>would set @state to "New York".
>
>In addition if @user['state'] was originally "California" I would hope that
>the above would still set @state to "California".
>
>
>
>

this is correct

@user["state"] = "New york"
@state = @user["state"].squeeze
p @state #=> "New york"

@user["state"] = "California"
@state = @user["state"].squeeze
p @state #=> "California"

>squeeze! - Squeezes str in place, returning either str, or nil if no
>changes were made.
>
>I interpret this as if no changes were made "nil" will be returned, so in my
>example of "California" wouldn't this set @state to nil?
>
>
>

correct

you would use squeeze if you wanted to modify @user["state"]. In which
case the following lines are about equivilent

@user["state"] = @user["state"].squeeze
@user["state"].squeeze!

--
Mark Sparshatt




 
Reply With Quote
 
Brian Tully
Guest
Posts: n/a
 
      06-22-2004

>
>> squeeze! - Squeezes str in place, returning either str, or nil if no
>> changes were made.
>>
>> I interpret this as if no changes were made "nil" will be returned, so in my
>> example of "California" wouldn't this set @state to nil?
>>
>>
>>

> correct
>
> you would use squeeze if you wanted to modify @user["state"]. In which
> case the following lines are about equivilent
>
> @user["state"] = @user["state"].squeeze
> @user["state"].squeeze!
>


Aha! Thanks Mark

That clears it up for me

Best regards,
brian




 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      06-24-2004
Brian Tully wrote:
> on 6/22/04 3:36 PM, Joel VanderWerf at (E-Mail Removed) wrote:
>
>
>>Brian Tully wrote:
>>
>>
>>>Should I use state.squeeze(" ") to ensure that it always returns the state
>>>even if nothing was changed? squeeze! seems to return nil if nothing needs
>>>to be changed.

>>
>>str.squeeze! is destructive (changes the String object referred to by
>>str), and str.squeeze is not. So it just depends on whether you need to
>>keep the original string intact (I'd guess not).

>
>
>
> Hmmm I'm a little hazy...
>
> I want Ruby to strip out any instances of multiple whitespace and return the
> "correct" string. I also want it to return the string regardless of whether
> the squeeze function modified it.
>
> So in these examples I would hope for the following:
>
> @state = @user['state'].squeeze(" ")
>
> if @user['state'] was originally "New York" I would hope that the above
> would set @state to "New York".
>
> In addition if @user['state'] was originally "California" I would hope that
> the above would still set @state to "California".


This is one way:

@state = @user['state']
@state.squeeze!(" ")

After this code, the string referenced by both @state and @user['state']
will be have no successive pairs of whitespace.


 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Splitting text at whitespace but keeping the whitespace in thereturned list MRAB Python 3 01-26-2010 11:36 PM
Structure using whitespace vs logical whitespace cmdrrickhunter@yaho.com Python 10 12-16-2008 03:51 PM
Whitespace-preservating Search & Replace in multiple XML documents François Robert XML 3 07-19-2005 04:01 PM
Whitespace where I don't want whitespace! Oli Filth HTML 9 01-17-2005 08:47 PM



Advertisments