0

I have an array of 10 items containing of a several lines string like

one string
two string
some string
any string

I want to delete lines containing words some and two. I made code like that:

search_text_domain = %r{some|two}
    groups_data.each do |line|
        line.each_line do |num|
        domain_users_name << (num) unless num =~ search_text_domain
        end
    end

It works fine but it puts all lines to one big array like domain_users_name = ["one string", "any string", "big string", "another_s....] and I want tu put it in array of arrays like

domain_users_name = [["one string", "any string"], ["big string", ""another_s...."], [........

I need version that permanently modify groups_data array. Any ideas?

2
  • And if one string of the subarray contains "some" or "two", should the whole subarray be discarded or should just that one string? Commented Feb 13, 2017 at 13:49
  • Just that one string. Commented Feb 13, 2017 at 14:07

2 Answers 2

1
input = ["one string\ntwo string\nsome string\nany string",
         "one string\ntwo string\nsome string\nany string"]


input.map { |a| a.split("\n").reject { |e| e =~ %r{some|two} } }

# or
# input.map { |a| a.each_line.map(&:strip).reject { |e| e =~ %r{some|two} } }

# or (smiley-powered version, see the method’s tail)
# input.map { |a| a.each_line.map(&:strip).reject(&%r{some|two}.method(:=~)) }

#⇒ [["one string", "any string"], ["one string", "any string"]]
Sign up to request clarification or add additional context in comments.

Comments

0

So you want to delete a group if one of the group elements matches the filter regexp?

groups = [['some', 'word'], ['other', 'word'], ['unrelated', 'list', 'of', 'things']]

filter = %r{word|some}
filtered = groups.delete_if do |group|
  group.any? do |word|
    word =~ filter
  end
end

p filtered

Does this do what you want?

1 Comment

Items of original array looks like array = ["one string\nsecond string", "other string\nother other string] - there are new lines. And I want to delete lines containing of specific words and after deleting e.g. line with wird secon get array = ["one string", "other string\nother other string"]

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.