What I’m trying to do is a little more complex then what my questions asks but I can’t put into words what it is I want to do and my terminology is probably wrong. Sorry. If I can edit the title of my question afterwards to better represent what I want to do I will do so.
I have an array that looks like this:
array = [
["bnb ><b>(BASE CABINET)</b>", "bnb ><b>(BASE CABINET)</b>", "1", "22\"", "22\"", "22\""]
["bnb >Edgebanding (sides)", "bnb >Edgebanding (sides)", "2", "22\"", "1/2\"", "1/2\""]
["bnb >Edgebanding (top/bottom)", "bnb >Edgebanding (top/bottom)", "2", "21\"", "1/2\"", "1/2\""]
["bnb >Panel (back)", "bnb >Panel (back)", "1", "21\"", "21\"", "1/2\""]
["bnb >Panel (sides)", "bnb >Panel (sides)", "2", "21 1/2\"", "21\"", "1/2\""]
["bnb >Panel (top/bottom)", "bnb >Panel (top/bottom)", "1", "22\"", "21 1/2\"", "1/2\""]
["bnb >Top (front/back)", "bnb >Top (front/back)", "2", "22\"", "3\"", "1/2\""]
["bnb >Top (sides)", "bnb >Top (sides)", "2", "15 1/2\"", "3\"", "1/2\""]
]
What I’m trying to do is get this as a result is basically removing everything after the > in the first element of each subarray and and remove everything before > in the second element of the subarrays:
array = [
["bnb", "<b>(BASE CABINET)</b>", "1", "22\"", "22\"", "22\""]
["bnb", "Edgebanding (sides)", "2", "22\"", "1/2\"", "1/2\""]
["bnb", "Edgebanding (top/bottom)", "2", "21\"", "1/2\"", "1/2\""]
["bnb", "Panel (back)", "1", "21\"", "21\"", "1/2\""]
["bnb", "Panel (sides)", "2", "21 1/2\"", "21\"", "1/2\""]
["bnb", "Panel (top/bottom)", "1", "22\"", "21 1/2\"", "1/2\""]
["bnb", "Top (front/back)", "2", "22\"", "3\"", "1/2\""]
["bnb", "Top (sides)", "2", "15 1/2\"", "3\"", "1/2\""]
]
and here’s what I’ve got so far as code:
val = 0
@test_list = []
test.each do |comp|
new_list = test[val]
new_list.map! { |element| element.gsub(/\s\>.*/, '') }
comp = comp[0]
@test_list.push(new_list)
val = val + 1
end
which removes everything after the > but it does it in both the first and second elements.
No need to use regular expressions when confronted with a problem 🙂 Sometimes a simple
String#splitwill do.