1

I have the following string:

str = "This is a string"

What I want to do is compare it with this array:

a = ["this", "is", "something"]

The result should be an array with "this" and "is" because both are present in the array and in the given string. "something" is not present in the string so it shouldn't appear. How can I do this?

3
  • 1
    What if a word occurs multiple times? In the string: s = "foo foo"; a = ["foo"] or in the array: s = "foo"; a = ["foo", "foo"] (or in both) Commented Aug 5, 2015 at 14:51
  • 1
    BTW, "this" and "This" are different strings. Commented Aug 5, 2015 at 14:58
  • 1
    Why not show us what you've tried, and we'll help correct it? Commented Aug 5, 2015 at 15:58

3 Answers 3

2

One way to do this:

str = "This is a string"
a = ["this","is","something"]
str.downcase.split & a
# => ["this", "is"]

I am assuming Array a will always have keys(elements) in downcase.

Sign up to request clarification or add additional context in comments.

1 Comment

does this change the array "a"?
1

There's always many ways to do this sort of thing

str = "this is the example string"
words_to_compare = ["dogs", "ducks", "seagulls", "the"]

words_to_compare.select{|word| word =~ Regexp.union(str.split) }
#=> ["the"]

Comments

0

Your question has an XY problem smell to it. Usually when we want to find what words exist the next thing we want to know is how many times they exist. Frequency counts are all over the internet and Stack Overflow. This is a minor modification to such a thing:

str = "This is a string"
a = ["this", "is", "something"]

a_hash = a.each_with_object({}) { |i, h| h[i] = 0 } # => {"this"=>0, "is"=>0, "something"=>0}

That defined a_hash with the keys being the words to be counted.

str.downcase.split.each{ |k| a_hash[k] += 1 if a_hash.key?(k) }
a_hash # => {"this"=>1, "is"=>1, "something"=>0}

a_hash now contains the counts of the word occurrences. if a_hash.key?(k) is the main difference we'd see compared to a regular word-count as it's only allowing word-counts to occur for the words in a.

a_hash.keys.select{ |k| a_hash[k] > 0 } # => ["this", "is"]

It's easy to find the words that were in common because the counter is > 0.

This is a very common problem in text processing so it's good knowing how it works and how to bend it to your will.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.