[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question regarding tr method in Strings Class

Michael W. Ryder

5/3/2007 4:29:00 AM

Is there a way to denote a null in the replacement for a character using
this method? For example, if I have a = "Jones,Ja'me" and I want to
convert the comma to a space and remove the apostrophe without placing a
character in there. I know I can use a.tr(",", " ").tr("'", "") to get
the desired result but would prefer to be able to say something like
a.tr(",'", " ") and have it remove the apostrophe and replace the comma
in one step. Besides, if I am going to be removing a lot of punctuation
the string could get very long and complicated.
9 Answers

Robert Dober

5/3/2007 4:43:00 AM

0

On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
> Is there a way to denote a null in the replacement for a character using
> this method? For example, if I have a = "Jones,Ja'me" and I want to
> convert the comma to a space and remove the apostrophe without placing a
> character in there. I know I can use a.tr(",", " ").tr("'", "") to get
> the desired result but would prefer to be able to say something like
> a.tr(",'", " ") and have it remove the apostrophe and replace the comma
> in one step. Besides, if I am going to be removing a lot of punctuation
> the string could get very long and complicated.
>
I do not think this works but why not use the "right" tool for each task
tr(","," ").gsub(/'/,"")
does not look too clumsy to me ;)

Cheers
Robert


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Peña, Botp

5/3/2007 4:50:00 AM

0

From: Michael W. Ryder [mailto:_mwryder@worldnet.att.net]
# in one step. Besides, if I am going to be removing a lot of
# punctuation the string could get very long and complicated.

delete and translate would do great

irb(main):001:0> "aasdfef".tr "ae","z"
=> "zzsdfzf"
irb(main):002:0> "aasdfef".tr "ae","zy"
=> "zzsdfyf"
irb(main):003:0> "aasdfef".delete "ae"
=> "sdff"


Michael W. Ryder

5/3/2007 5:13:00 AM

0

Robert Dober wrote:
> On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
>> Is there a way to denote a null in the replacement for a character using
>> this method? For example, if I have a = "Jones,Ja'me" and I want to
>> convert the comma to a space and remove the apostrophe without placing a
>> character in there. I know I can use a.tr(",", " ").tr("'", "") to get
>> the desired result but would prefer to be able to say something like
>> a.tr(",'", " ") and have it remove the apostrophe and replace the comma
>> in one step. Besides, if I am going to be removing a lot of punctuation
>> the string could get very long and complicated.
>>
> I do not think this works but why not use the "right" tool for each task
> tr(","," ").gsub(/'/,"")
> does not look too clumsy to me ;)
>
> Cheers
> Robert
>
>
Your solution is about the same as the solution I found "clumsy". I can
use a.tr to remove multiple different characters such as: a ="This is a
test, this is only a test!". a.tr(",!", "") will remove the comma and
exclamation mark. So why can't I use the same method to remove the
comma and replace the exclamation mark with a period? I know that there
are escape codes for things like tabs and new-lines, so I was wondering
if there was one for a null character.

Robert Dober

5/3/2007 5:21:00 AM

0

On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
<cut>
> Your solution is about the same as the solution I found "clumsy".
You are not coming from a Unix background, are you? We are used to
pasting things together like that, but I respect your taste.
> I can
> use a.tr to remove multiple different characters such as: a ="This is a
> test, this is only a test!". a.tr(",!", "") will remove the comma and
> exclamation mark. So why can't I use the same method to remove the
> comma and replace the exclamation mark with a period? I know that there
> are escape codes for things like tabs and new-lines, so I was wondering
> if there was one for a null character.
I understood that you wanted to delete a character not to replace it
with a null character, which should be possible via \000. But I fail
to see the purpose of this in the context of your question.
Robert
>
>


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Brian Candler

5/3/2007 7:41:00 AM

0

On Thu, May 03, 2007 at 01:30:06PM +0900, Michael W. Ryder wrote:
> Is there a way to denote a null in the replacement for a character using
> this method? For example, if I have a = "Jones,Ja'me" and I want to
> convert the comma to a space and remove the apostrophe without placing a
> character in there. I know I can use a.tr(",", " ").tr("'", "") to get
> the desired result but would prefer to be able to say something like
> a.tr(",'", " ") and have it remove the apostrophe and replace the comma
> in one step. Besides, if I am going to be removing a lot of punctuation
> the string could get very long and complicated.

Removing the apostrophe, and replacing it with null, are two different
things. But you could use the null as a placeholder and then remove it
afterwards:

a = "Jones,Ja'me"
a.tr(",'"," \000").delete("\000")

No matter how much punctuation you want to remove, it's still only two
steps.

If you want this in a single step, hmm, how about:

REPLACE = {
"," => " ",
"'" => "",
}
REPLACE_RE = /#{REPLACE.keys.map {|k| Regexp.escape(k) }.join("|")}/

a.gsub(REPLACE_RE) { |m| REPLACE[m] }

It might look a bit long-winded, but the constant initialisation only has to
be done once at the top of your program, and it can replace arbitary
sequences of characters with other arbitary sequences.

HTH,

Brian.

Michael W. Ryder

5/3/2007 7:05:00 PM

0

Robert Dober wrote:
> On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
> <cut>
>> Your solution is about the same as the solution I found "clumsy".
> You are not coming from a Unix background, are you? We are used to
> pasting things together like that, but I respect your taste.
> > I can
>> use a.tr to remove multiple different characters such as: a ="This is a
>> test, this is only a test!". a.tr(",!", "") will remove the comma and
>> exclamation mark. So why can't I use the same method to remove the
>> comma and replace the exclamation mark with a period? I know that there
>> are escape codes for things like tabs and new-lines, so I was wondering
>> if there was one for a null character.
> I understood that you wanted to delete a character not to replace it
> with a null character, which should be possible via \000. But I fail
> to see the purpose of this in the context of your question.
> Robert
>>
>>
>
>
I find your sig very appropriate. I can find several ways to clean up a
string using two or more operations, I was just looking to see if there
was not also a way to do the same thing with one operation.
In some ways Ruby is far easier to use than the language I have been
using for over 25 years, but there are other places it is much harder.
I am doing a lot of experimenting to find out the best way for me to do
things and this is the latest experiment.

Rick DeNatale

5/4/2007 1:35:00 PM

0

On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:


> In some ways Ruby is far easier to use than the language I have been
> using for over 25 years, but there are other places it is much harder.

Just out of curiosity, what language is that, and how does it make
this particular operation much easier than Ruby does?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Brian Candler

5/4/2007 6:34:00 PM

0

On Fri, May 04, 2007 at 10:47:15PM +0900, Logan Capaldo wrote:
> On 5/3/07, Brian Candler <B.Candler@pobox.com> wrote:
> >
> >
> >If you want this in a single step, hmm, how about:
> >
> >REPLACE = {
> > "," => " ",
> > "'" => "",
> >}
> >REPLACE_RE = /#{REPLACE.keys.map {|k| Regexp.escape(k) }.join("|")}/
>
>
> Tweak:
> REPLACE_RE = Regexp.union(*REPLACE.keys)

Ooh, shiny.

Michael W. Ryder

5/4/2007 6:57:00 PM

0

Rick DeNatale wrote:
> On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
>
>
>> In some ways Ruby is far easier to use than the language I have been
>> using for over 25 years, but there are other places it is much harder.
>
> Just out of curiosity, what language is that, and how does it make
> this particular operation much easier than Ruby does?
>

I have been using Business Basic professionally for over 25 years after
learning Fortran and Basic in college. String handling is much easier
in Ruby once I get past the small differences, but in screen displays,
file handling, and exception handling Ruby is not even on the horizon.
These are all built into the language and are very easy to use. As an
example, that I am working on in another thread, to print $12,345.67 in
Business Basic in the middle of a screen I enter 'Print
@(20,20),A:"$##,###,##0.00". This same statement can handle any amount
from 1 cent to 99 million dollars and will keep the decimal point lined
up for amounts above and below that line.
For this thread, Ruby is much better as in Business Basic I have to find
the position of a character, replace it or remove it, and repeat. In
Ruby this can all be done with one tr or gsub call.