2016-05-31 42 views

回答

0

我將把圖像1作爲表1和圖像2作爲表2.

在表2中的一行由水平,1類,2類,...,X類 。我們可以看到,你需要爲每個類別一欄,所以你要在這裏做的第一件事就是從表中獲取的一組類別的1

require 'set' 

# A set of all the categories extracted from table 1 
categories = Set.new 

table1 = [ 
    { title: 'Item 1', level: 1, category: 'Category 1' }, 
    { title: 'Item 2', level: 1, category: 'Category 2' }, 
    ... 
] 

# Extract categories from table 1 
table1.each do |item| 
    categories << item[:category] # add category to set 
end 

,我們可以在這裏看到的下一件事是,在表2項目按其級別和類別排列。我們需要一種方式來說「給我的項目(不止一種可能)在X級與Y類」。因此,表2可以表示類似如下:

table2 = { 
    level1: { category1: [...], category2: [...], category3: [...] }, 
    level2: { category1: [...], category2: [...], category3: [...] }, 
} 

舉個例子,這樣,你會使用

table2[:level1][:category2] 

下一個任務是從錶轉換得到(1級,2類)項目1到表2.考慮到表2的上述表示,這很簡單,並且可以通過遍歷表1並將該項(或者其標題)添加到table2散列的適當的級別和類別數組中來完成。

# Arrange items by level and category 
table1.each do |item| 
    table2[item[:level]] ||= {} # hash of categories for this item's level 
    table2[item[:level]][item[:category]] ||= [] # array of items for this item's level and category 
    table2[item[:level]][item[:category]] << item[:title] # add item to its corresponding array 
end 

...你現在有你想要的表示你的數據。

表2中要注意的一件事是(級別1,類別1)有兩個項目,並使用兩個單獨的行來表示這一點。如果你想這樣輸出,那麼你需要遍歷每個級別,併爲每個級別遍歷每個類別,在每個項目在(級別,類別)打印一行。你可以看到如何做到這一點在下面的完整的源代碼的例子(你是免費的,如果你想使用):

require 'set' 

table1 = [ 
    { title: 'Item 1', level: 1, category: 'Category 1' }, 
    { title: 'Item 2', level: 1, category: 'Category 2' }, 
    { title: 'Item 3', level: 2, category: 'Category 3' }, 
    { title: 'Item 5', level: 3, category: 'Category 3' }, 
    { title: 'Item 6', level: 1, category: 'Category 1' }, 
    { title: 'Item 7', level: 2, category: 'Category 2' }, 
    { title: 'Item 8', level: 4, category: 'Category 1' }, 
    { title: 'Item 9', level: 4, category: 'Category 3' } 
] 

table2 = { 
    # 1: { category1: [...], category2: [...], category3: [...] }, 
    # 2: { category1: [...], category2: [...], category3: [...] }, 
} 

# A set of all the categories extracted from table 1 
categories = Set.new 

# Extract categories and also arrange items by level and category 
table1.each do |item| 
    categories << item[:category] # add category to set 

    table2[item[:level]] ||= {} # hash of categories for this item's level 
    table2[item[:level]][item[:category]] ||= [] # array of items for this item's level and category 
    table2[item[:level]][item[:category]] << item[:title] # add item to its corresponding array 
end 

# Print headers for table 1 
puts 
puts 'Title | Level | Category' 

# Print rows for table 1 
table1.each do |item| 
    print item.values 
    puts 
end 

# Print headers for table 2 
puts 
print 'Level | ' 
categories.each do |category| 
    print category + ' | ' 
end 
puts 

# Print rows for table 2 
table2.each do |level, categorised_items| 
    row = 0 

    loop do 
    last_row = true 

    # level for this row 
    print level 
    print ' | ' 

    # print item for each category 
    categories.each do |category| 
     if categorised_items[category] && categorised_items[category][row] 
     print categorised_items[category][row] + ' | ' 

     # check if an item exists for the next row 
     last_row = false if categorised_items[category][row + 1] 
     else 
     print ' | ' 
     end 
    end 

    puts 

    break if last_row 

    row += 1 
    end 
end 

輸出:

Title | Level | Category 
["Item 1", 1, "Category 1"] 
["Item 2", 1, "Category 2"] 
["Item 3", 2, "Category 3"] 
["Item 5", 3, "Category 3"] 
["Item 6", 1, "Category 1"] 
["Item 7", 2, "Category 2"] 
["Item 8", 4, "Category 1"] 
["Item 9", 4, "Category 3"] 

Level | Category 1 | Category 2 | Category 3 | 
1 | Item 1 | Item 2 | | 
1 | Item 6 | | | 
2 | | Item 7 | Item 3 | 
3 | | | Item 5 | 
4 | Item 8 | | Item 9 | 
+0

謝謝cthorne,我會申請你的解決方案,並回到你身邊:) – Yousef