Method to search an array for specific strings

Discussion in 'Ruby' started by Uros, Apr 12, 2006.

  1. Uros

    Uros Guest

    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..
    :)
    Uros, Apr 12, 2006
    #1
    1. Advertising

  2. Uros

    vsv Guest

    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
    vsv, Apr 12, 2006
    #2
    1. Advertising

  3. 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
    Robert Klemme, Apr 12, 2006
    #3
  4. Uros

    Uros Guest

    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
    Uros, Apr 12, 2006
    #4
  5. 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
    Robert Klemme, Apr 12, 2006
    #5
  6. Uros

    Uros Guest

    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, Apr 12, 2006
    #6
  7. Uros

    Uros Guest

    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
    Uros, Apr 13, 2006
    #7
  8. 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
    Robert Klemme, Apr 13, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmF2?=

    Is ViwState Page-Specific or UserControl-Specific

    =?Utf-8?B?SmF2?=, Aug 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    510
    =?Utf-8?B?SmF2?=
    Aug 16, 2006
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    724
    Malcolm
    Jun 24, 2006
  3. mazdotnet
    Replies:
    2
    Views:
    378
    Alexey Smirnov
    Oct 2, 2009
  4. Abby Lee
    Replies:
    5
    Views:
    366
    Abby Lee
    Aug 2, 2004
  5. William FERRERES
    Replies:
    7
    Views:
    198
    William FERRERES
    Jul 9, 2007
Loading...

Share This Page