[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simple programming q

grrr

2/14/2006 11:29:00 PM

I am a ruby newbie. I want to make a simple script that replaces certain
characters in a block of text with specific other characters.

I am wondering what kind of loop or algo is thought to be most effective
for this kind of work..
Maybe go through the block of text with a hashing table and replace
each 'hit'? Would this be reasonable approach?

#example pseudocode idea
blokoftxt = 'Theres some text in block.'
replacekey = { 'T' = 'W', 'a' = 'o', 'o' = 'a' }

blokoftxt.each ( if equal to a replacekey, replace with key value )


This would mean going through each character in the textblock. I guess one
could conversely go through the replacekey array and match to the
textblock?? Which way to go?

J Von Rubiginner


5 Answers

Bill Guindon

2/14/2006 11:44:00 PM

0

On 2/14/06, grrr <grrr@toto.maatti> wrote:
> I am a ruby newbie. I want to make a simple script that replaces certain
> characters in a block of text with specific other characters.
>
> I am wondering what kind of loop or algo is thought to be most effective
> for this kind of work..
> Maybe go through the block of text with a hashing table and replace
> each 'hit'? Would this be reasonable approach?
>
> #example pseudocode idea
> blokoftxt = 'Theres some text in block.'
> replacekey = { 'T' = 'W', 'a' = 'o', 'o' = 'a' }
>
> blokoftxt.each ( if equal to a replacekey, replace with key value )

Here's a simple answer, using a regular expression.

blokoftxt = 'Theres some text in block.'
replacekey = { 'T' => 'W', 'a' => 'o', 'o' => 'b' }
replacekey.each {|key, val| blokoftxt.gsub!(/#{key}/, val)}

> This would mean going through each character in the textblock. I guess one
> could conversely go through the replacekey array and match to the
> textblock?? Which way to go?
>
> J Von Rubiginner
>
>
>
>


--
Bill Guindon (aka aGorilla)
The best answer to most questions is "it depends".


Eric Hodel

2/14/2006 11:54:00 PM

0

On Feb 14, 2006, at 3:33 PM, grrr wrote:

> I am a ruby newbie. I want to make a simple script that replaces
> certain
> characters in a block of text with specific other characters.
>
> I am wondering what kind of loop or algo is thought to be most
> effective
> for this kind of work..
> Maybe go through the block of text with a hashing table and replace
> each 'hit'? Would this be reasonable approach?
>
> #example pseudocode idea
> blokoftxt = 'Theres some text in block.'
> replacekey = { 'T' = 'W', 'a' = 'o', 'o' = 'a' }
>
> blokoftxt.each ( if equal to a replacekey, replace with key value )

blokoftxt.tr 'Tao', 'Woa'

--
Eric Hodel - drbrain@segment7.net - http://se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...




Tim Hunter

2/14/2006 11:59:00 PM

0

grrr wrote:
> I am a ruby newbie. I want to make a simple script that replaces certain
> characters in a block of text with specific other characters.
>
> I am wondering what kind of loop or algo is thought to be most effective
> for this kind of work..
> Maybe go through the block of text with a hashing table and replace
> each 'hit'? Would this be reasonable approach?
>
> #example pseudocode idea
> blokoftxt = 'Theres some text in block.'
> replacekey = { 'T' = 'W', 'a' = 'o', 'o' = 'a' }
>
> blokoftxt.each ( if equal to a replacekey, replace with key value )
>
>
> This would mean going through each character in the textblock. I guess one
> could conversely go through the replacekey array and match to the
> textblock?? Which way to go?
>
> J Von Rubiginner
>
>


text = <<-ENDTEXT
The Owl and the Pussy-cat went to sea
In a beautiful pea green boat,
They took some honey, and plenty of money,
Wrapped up in a five pound note.
The Owl looked up to the stars above,
And sang to a small guitar,
'O lovely Pussy! O Pussy my love,
What a beautiful Pussy you are,
You are,
You are!
What a beautiful Pussy you are!'
ENDTEXT

replace_key = { 'O' => 'd', 'P' => 'w', 'b' => 'x'}
new_text = text.gsub(/[#{replace_key.keys.join}]/) {|match|
replace_key[match]
}
puts new_text

The result is:

The dwl and the wussy-cat went to sea
In a xeautiful pea green xoat,
They took some honey, and plenty of money,
Wrapped up in a five pound note.
The dwl looked up to the stars axove,
And sang to a small guitar,
'd lovely wussy! d wussy my love,
What a xeautiful wussy you are,
You are,
You are!
What a xeautiful wussy you are!'

Harold Hausman

2/15/2006 1:13:00 AM

0

Haha! That's awesome. (sorry, I'm easily amused)

-Harold

> >
> > #example pseudocode idea
> > blokoftxt = 'Theres some text in block.'
> > replacekey = { 'T' = 'W', 'a' = 'o', 'o' = 'a' }
> >
> > blokoftxt.each ( if equal to a replacekey, replace with key value )
>
> blokoftxt.tr 'Tao', 'Woa'
>
> --
> Eric Hodel


grrr

2/15/2006 11:44:00 AM

0

Re:
>> Eric Hodel:
>> blokoftxt.tr 'Tao', 'Woa'

On Wed, 15 Feb 2006 10:12:41 +0900, Harold Hausman wrote:
> Haha! That's awesome. (sorry, I'm easily amused)
>
> -Harold

Yes indeed! Hehe I guess I am easily amused, too!

Thats pretty much the solution I was looking for, even if I was expecting
replies from Knuths books. Thanks Eric!