Method to search an array for specific strings

U

Uros

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..
:)
 
V

vsv

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
 
R

Robert Klemme

Uros said:
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
 
U

Uros

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
 
R

Robert Klemme

Uros said:
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
 
U

Uros

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
 
U

Uros

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
 
R

Robert Klemme

Uros said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top