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
···
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.
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
# 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"
···
From: Michael W. Ryder [mailto:_mwryder@worldnet.att.net]
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.
···
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.
Robert Dober 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 meCheers
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.
···
On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
Tweak:
REPLACE_RE = Regexp.union(*REPLACE.keys)
···
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("|")}/
<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
···
On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
Ooh, shiny.
···
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)
Robert Dober 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 canuse 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.
···
On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:
Just out of curiosity, what language is that, and how does it make
this particular operation much easier than Ruby does?
···
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.
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
Rick DeNatale 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.
···
On 5/3/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote: