[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Method to search an array for specific strings

Uros

4/12/2006 7:34:00 AM

Dear ruby users,

I have stumbled upon a problem in my first ruby program. Upon searching
I still could not find a solution, I guess it's my lack of knowledge to
blame.

What I'm trying to achive is opening a text file, then search the text
file for specific strings and then replacing that strings with other
strings. I created an array with original strings, and and array with
replaced strings. The array are of the same size. Then once the strings
are replaced, the new file is to be writen.

I can't figure out what would be a good procedure for searching for a
specific string in an array.. all the other things I need to do, I
think I can manage...

Anyone can help me with that? Oh, did I mention that I'm new to ruby..
:-)

8 Answers

Sergey Volkov

4/12/2006 8:42:00 AM

0

I'm not Ruby expert, but this may work for you:
---------------
$ cat tGsub_a.rb
class String
# define new String method for 'array' substitutions
def gsub_a originals, replacements
# create replacements table
orig2repl = Hash[ *originals.zip( replacements ).flatten ]
# regexp matching any original
regexp = /#{originals.map{ |s| Regexp.escape s }.join( '|' )}/
# substitute each original with replacement from table
self.gsub( regexp ){ |orig| orig2repl[ orig ] }
end
end
puts "AB[^1-2$].XY".gsub_a(
["AB", "1-2", "XY", "."],
["CC", "99", "ZZ", "+"]
)
$ ruby tGsub_a.rb
CC[^99$]+ZZ
---------------
enjoy Rubying!
Sergey Volkov

Robert Klemme

4/12/2006 9:31:00 AM

0

Uros wrote:
> Dear ruby users,
>
> I have stumbled upon a problem in my first ruby program. Upon searching
> I still could not find a solution, I guess it's my lack of knowledge to
> blame.
>
> What I'm trying to achive is opening a text file, then search the text
> file for specific strings and then replacing that strings with other
> strings. I created an array with original strings, and and array with
> replaced strings. The array are of the same size. Then once the strings
> are replaced, the new file is to be writen.
>
> I can't figure out what would be a good procedure for searching for a
> specific string in an array.. all the other things I need to do, I
> think I can manage...

Better use a Hash for mapping strings, that's the natural data structure
for doing this as lookups are far more efficient.

You could do something like this if your input is line oriented and you
want to replace words:

# create a hash that will return the key if not found
repl = Hash.new {|h,k| k}.update(
"someword" => "replacement word",
"stuff" => "buff"
)

File.open("foo") do |io|
io.each_line do |line|
# replace all words
puts( line.gsub(/\w+/) {|m| repl[m]} )
end
end

Kind regards

robert

Uros

4/12/2006 12:03:00 PM

0

Thanks for all the help... so this is how I did it...I still have to
look up what the gsub stuff is all about, but, hey it's working... :-)

require "fileutils"
nr_installs=9
FileUtils.cd('dragonfly')

for i in 1...nr_installs+1

FileUtils.mkdir('dragonfly_'+i.to_s)

f =
File.open("c:/ruby-devel/dragonfly/dragonfly_"+i.to_s+"/config.php",
"w")

repl = Hash.new {|h,k| k}.update("dragonfly_1" => "dragonfly_"+i.to_s)
File.open("c:/ruby-devel/config.php") do |io|
io.each_line do |line|
f.write(line.gsub(/\w+/) {|m| repl[m]} )
end
end
end

Robert Klemme

4/12/2006 12:17:00 PM

0

Uros wrote:
> Thanks for all the help... so this is how I did it...I still have to
> look up what the gsub stuff is all about, but, hey it's working... :-)
>
> require "fileutils"
> nr_installs=9
> FileUtils.cd('dragonfly')
>
> for i in 1...nr_installs+1
>
> FileUtils.mkdir('dragonfly_'+i.to_s)
>
> f =
> File.open("c:/ruby-devel/dragonfly/dragonfly_"+i.to_s+"/config.php",
> "w")
>
> repl = Hash.new {|h,k| k}.update("dragonfly_1" => "dragonfly_"+i.to_s)
> File.open("c:/ruby-devel/config.php") do |io|
> io.each_line do |line|
> f.write(line.gsub(/\w+/) {|m| repl[m]} )
> end
> end
> end

You do not close f. I'd change that to use the block form:

File.open("c:/ruby-devel/dragonfly/dragonfly_"+i+"/config.php","w") do |f|
....
end

Also, if you just need to replace a single string then the approach with
the array is completely overdone. You can do that with a single rx
which is also more efficiently:

io.each_line do |line|
f.puts( line.gsub(/dragonfly_\d+/, "dragonfly_#{i}") )
end

It's not clear to me why you originally stated that you need to replace
several strings.

robert

Uros

4/12/2006 12:23:00 PM

0

Thanks for the sugestions.

This code example was just a little test, I still need to replace
several strings. I just thought to test the code with just one. Guess I
forgot to mention that.. :)

lp,
uros

Uros

4/13/2006 5:52:00 AM

0

Just another quick question.. the code is working, but how do I replace
a string that has a space in it.. the code looks something like this..
---snip--
repl = Hash.new {|h,k| k}.update("mambo_1" => "mambo_"+i.to_s,
"mambo1" => "mambo"+i.to_s,
"Mambo test" => "Mambo stran st."+i.to_s
)
---snip---

The "Mambo test" string doesn't get replaced.. any quick fix for that?

Thanks,

Uros

Robert Klemme

4/13/2006 10:11:00 PM

0

Uros wrote:
> Just another quick question.. the code is working, but how do I replace
> a string that has a space in it.. the code looks something like this..
> ---snip--
> repl = Hash.new {|h,k| k}.update("mambo_1" => "mambo_"+i.to_s,
> "mambo1" => "mambo"+i.to_s,
> "Mambo test" => "Mambo stran st."+i.to_s
> )
> ---snip---
>
> The "Mambo test" string doesn't get replaced.. any quick fix for that?

No. It's difficult to tell without more information about the strings
to replace. You'll probably have to specialize the regexp. As a
generic solution you can do something like this:

rx = Regexp.new( repl.keys.map {|k| Regexp.quote k}.join '|' )

Kind regards

robert

Salah Gencet

5/8/2011 5:58:00 AM

0

Rasul Paul <Rasul@rasul.com> wrote in news:iq509j$ohq$1@speranza.aioe.org:

> What will the Densus 88 find in your house, or what will they find in
> Gatho's house, or in MekoQ's house, or in Konsisten's house in case
> they, or the German's police should break into your house tonight?
> Have you ever thought about that seriously?

just in order to give false impression to public that this fake Rasul Paul
is a new comer to SCI, so he didnt touch mbiLL's name in his posts


wakakakakakk since when a genuine new comer knows that someone with

nick "mekoQ" exists in this board ?????