我想要做的事情比我的问题要复杂得多一些,但我不能说出我想要做的事情,我的术语可能是错误的。抱歉。如果我之后可以编辑我的问题的标题,以便更好地代表我想要做的事情,我会这样做。
我有一个如下所示的数组:
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\""]
]
我想要做的是得到这个结果基本上删除每个子数组的第一个元素中的>
之后的所有内容,并删除第二个元素中>
之前的所有内容。子阵列:
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\""]
]
这就是我到目前为止的代码:
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
删除>
之后的所有内容,但它会在第一个和第二个元素中执行。
答案 0 :(得分:1)
遇到问题时无需使用正则表达式:)有时简单的String#split
会这样做。
source_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\""]
]
target_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\""]
]
arr = source_array.map do |a|
[
a[0].split('>').first.strip,
a[1].split('>', -1)[1..-1].join('>')
] + a[2..-1]
end
arr == target_array # => true
答案 1 :(得分:0)
Array.each_with_index
可能就是你想要的。通过索引检查,我们可以确定它是第一个还是第二个
arr=[
["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\""]
]
new_arr =
arr.map do |sub_arr|
sub_arr.each_with_index.map do |str,i|
if i == 0
str.gsub(/\s\>.*/,'')
elsif i == 1
str.gsub('bnb >','')
else
str
end
end
end
p new_arr
这是一个有效的初步解决方案。您可以随时将其转换为您自己的样式,例如,更改do end to brace,使用map !,将if语句更改为同一行等等。