1

I'm trying to isolate certain strings in an array, e.g.,

["banana man", "apple", "banana woman"]

which are identifiable by the start of the string (i.e. substring). I want to keep 'banana man' and 'banana woman' but remove 'apple'. Any help would be appreciated.

5
  • 2
    Do you know the prefix (e.g. "banana") in advance or do you have to extract / identify it from the given strings? Commented Oct 13, 2015 at 15:40
  • 2
    Issue is not clear at all. In which way is "apple" isolated from the other two elements? Is it because it starts with "a"? Commented Oct 13, 2015 at 15:40
  • 1
    Beth, if a is your array and we interpret your question literally (as we should), a.delete("apple") is sufficient (though I doubt that's what you are looking for). Commented Oct 13, 2015 at 16:08
  • "Bananaman" is one word. There's also a Banana Kid. Commented Oct 13, 2015 at 16:48
  • Issue was that I had a whole bunch of html returned when I curl a URL and all I wanted were the object names inside that mess. Commented Oct 16, 2015 at 13:19

4 Answers 4

9

This is a great usecase for grep

ary = ["banana man", "apple", "banana woman"]
ary.grep(/^banana/) # => ["banana man", "banana woman"]
Sign up to request clarification or add additional context in comments.

2 Comments

While grep results in a visually simple expression, the benchmark shows it's costly.
Thanks, this worked brilliantly, performance hasn't been raised as an issue yet for the program so I'll keep using grep.
3

Try start_with? and select to choose just those that start with the string you want:

["banana man", "apple", "banana woman"].select { |i| i.start_with?("bana") }
=> ["banana man", "banana woman"]

Comments

3

Benchmark time:

require 'fruity'

ARY = ["banana man", "apple", "banana woman"]

ARY.grep(/^banana/) # => ["banana man", "banana woman"]
ARY.select { |i| i.start_with?("bana") } # => ["banana man", "banana woman"]

compare do
  grep_only { ARY.grep(/^banana/) }
  select_start_with { ARY.select { |i| i.start_with?("bana") } }
end
# >> Running each test 4096 times. Test will take about 1 second.
# >> select_start_with is faster than grep_only by 3x ± 1.0

Extending ARY:

ARY = ["banana man", "apple", "banana woman"] * 1000

compare do
  grep_only { ARY.grep(/^banana/) }
  select_start_with { ARY.select { |i| i.start_with?("bana") } }
end
# >> Running each test 8 times. Test will take about 1 second.
# >> select_start_with is faster than grep_only by 3x ± 0.1

2 Comments

Thank you for the benchmarks. it is funny the next fastest method (~ half speed) I can find is actually join and scan (which seems error prone depending on the specifications).
Then add that into the mix. It's a community wiki for a reason.
0

At the Tin Mans behest, and due to the fact that your actual specifications are very vague, I will add this alternative solution as a possible but potentially error prone option.

ARY = ["banana man", "apple", "banana woman"] 
ARY.join('  ').scan(/banana\s\w+/)
#=> ["banana man", "banana woman"]

This will out perform grep but is about half the speed of select and start_with? combination and is based solely on the post and not your potentially underlying intentions.

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.