Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 17th, 2009, 11:57 AM
PhdMama PhdMama is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 5 PhdMama User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 5 sec
Reputation 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.

Reply With Quote
  #2  
Old August 17th, 2009, 01:24 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
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

Reply With Quote
  #3  
Old August 25th, 2009, 10:12 PM
risctakr risctakr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 1 risctakr Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 1 h 55 m 56 sec
Reputation Power: 0
Lets see if this helps ...

Quote:
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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Variables in Loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap