#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    5
    Rep Power
    0

    Variables in Loop


    Probably a stupid question but can't get the right direction. The program is supposed to read some text and check if words exist in this text:

    keywords.each{|keyword|
    test = "bla bla ........ bla".match(/#{keyword}/)
    if test != nil
    puts test[1]
    end
    }

    How do i get it to put a 1 in the variable keyword if it finds it and 0 otherwise - i.e. put a 1 in @money if it finds "money", etc. In reality I have an array of 200 keywords, and, yeah, i need all 200 of those variables because i'm going to mix and match them in various ways.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    I'm not sure the benefits of either approach, you would need to do the empirical study yourself, but there are a couple of ways.

    Instead of looping over the set of keys and searching the string (200+) times, you can just search the string once with a rather large regular expression.
    Code:
    str = "This string has twice the number of ints as longs (int)"
    keys = ["int", "long", "double"]
    found = Hash.new(false) # unbound indexes return false, not nil
    str.scan(/#{keys.join('|')}/) { |x| found[x] = true }
    
    # => {"int"=>true, "long"=>true}
    Realize that this matches words within words (take note that it found long from longs).

    Or you can do the loop the way you designed
    Code:
    str = "This string has twice the number of ints as longs (int)"
    keys = ["int", "long", "double"]
    found = Hash.new(false) # unbound indexes return false, not nil
    keys.each do |key|
       found[key] = /#{key}/.match(str) ? true : false
    end
    
    # => {"int"=>true, "double"=>false, "long"=>true}
    Again, with the word within words problem but notice also the subtle difference in the resulting hash. The second method above explicitly sets a value for all keys and not just the ones found. This may or may not make your post processing any harder.

    With regard to the word within words dilemma (which you might have considered already), you can provide extra search criteria to the regular expression to limit by word or space boundaries. Doing so necessarily changes the data (and possibly its format) returned from the match/scan call.
    Read up on the String#scan, Regexp#match, and Regexp class for more information.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Lets see if this helps ...


    Originally Posted by PhdMama
    Probably a stupid question but can't get the right direction. The program is supposed to read some text and check if words exist in this text:

    keywords.each{|keyword|
    test = "bla bla ........ bla".match(/#{keyword}/)
    if test != nil
    puts test[1]
    end
    }
    keywords = ['house', 'home', 'garage']

    keywords.each{|keyword|
    if "This house is my home.".match(/#{keyword}/)
    puts "Match on #{keyword}."
    end
    }


    The above prints out "Match on house." and "Match on home."
    I think this should fix your flow controll problem.

    Comments on this post

    • L7Sqr disagrees : Where was there mention of flow control problems?

IMN logo majestic logo threadwatch logo seochat tools logo