2012-04-14 82 views
-1

我想要做的是稍微複雜一些,然後我的問題提出了一些問題,但我無法將它放入文字中,我想要做什麼以及我的術語可能是錯誤的。抱歉。如果我以後可以編輯我的問題的標題,以更好地表達我想要做的事情,我會這樣做。紅寶石陣列,在子陣列內刪除所有東西

我有一個數組,看起來像這樣:

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 

刪除>之後的所有內容,但它在第一個和第二個元素中都執行。

回答

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 
+0

謝謝塞爾吉奧!只是傾斜新的東西:) – JoMojo 2012-04-14 02:28:47

+0

@FrankN:究竟是什麼? :) – 2012-04-14 02:29:35

+0

我還是很新的編程,我從來沒有使用過String#split,甚至是新的它:) – JoMojo 2012-04-14 02:32:40

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 

這是一個可行的初步解決方案。您可以隨時將其轉變爲您自己的風格,例如,更改結束大括號,使用地圖!,如果語句更改爲同一行,等等。

+0

謝謝texasbruce ...我也會試試看看我還能學什麼正在進行中 ;) – JoMojo 2012-04-14 02:30:06