2011-05-10 76 views
0

我是Ruby的新手,因爲現在可能大家都知道了:) 我對某個服務有一個查詢,我找回了一個數組。當我運行這段代碼解析Ruby數組

@query_result.each do |test| 
    puts test 
end 

我得到的正是這種輸出

["names", ["s", "label"]] ["values", [["<http://www.udfr.org/test-instance#PDF-1>", "\"Acrobat PDF 1.0 - Portable Document Format\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#BroadcastWave>", "\"Broadcast WAVE\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#PNG-1>", "\"Portable Network Graphics\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#PNG-1-1>", "\"Portable Network Graphics\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#GIF-1989a>", "\"Graphics Interchange Format\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#TIFF-4>", "\"Tagged Image File Format\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#TIFF-6>", "\"Tagged Image File Format\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#BroadcastWave-1>", "\"Broadcast WAVE\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#PNG-1-2>", "\"Portable Network Graphics\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#TIFF-3>", "\"Tagged Image File Format\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#TIFF-5>", "\"Tagged Image File Format\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#AVI-Generic>", "\"Audio/Video Interleaved Format\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#GIF-1987a>", "\"Graphics Interchange Format\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#WaveformAudio>", "\"Waveform Audio\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#BroadcastWave-1>", "\"Broadcast WAVE\"^^<http://www.w3.org/2001/XMLSchema#string>"], ["<http://www.udfr.org/test-instance#BroadcastWave>", "\"Broadcast WAVE\"^^<http://www.w3.org/2001/XMLSchema#string>"]]]

我知道它是神祕的,但基本上我只需要在這些提取值:

names 
s 
label 
values 

從數組中獲取列的實際值的代碼是什麼?

+1

你只是試圖獲得這個數組的前四個值,或者你是否想要做其他事情? – dogenpunk 2011-05-10 18:22:04

+0

你必須提及什麼是@query_result,你想要什麼。 – shajin 2011-05-10 18:29:53

+0

@dogenpunk不,這些不是4個值。這些是指標。我有一個名爲「label」的列和一個名爲「s」的列 - 我只是試圖爲它們提取值。 – GeekedOut 2011-05-10 18:31:19

回答

4

@query_result具有結構:

[["names", ["s", "label"]], ["values", array]] 

其中array包括對。我沒有看到字面字符串"names","s","label""values"有用。你可能想要拿出array

如果你

@query_result[1] 

這會給你的@query_result第二個元素,這是

["values", array] 

如果進一步做

@query_result[1][1] 

這會給你的array部分:

[ 
    [ 
    "http://www.udfr.org/test-instance#PDF-1", 
    "\"Acrobat PDF 1.0 - Portable Document Format\"^^http://www.w3.org/2001/XMLSchema#string" 
    ], 
    [ 
    "http://www.udfr.org/test-instance#BroadcastWave", 
    "\"Broadcast WAVE\"^^http://www.w3.org/2001/XMLSchema#string" 
    ], 

    ... 

] 
+0

是的,[1,1]做到了。它與其他語言不同,你必須不斷增加循環的計數器。 put是否取出當前的數組元素,這就是爲什麼[1,1]總是指向當前值? – Genadinik 2011-05-10 19:12:25

+0

這不是'[1,1]'。這是'[1] [1]'。它意味着原始數組第二項的第二項(請注意索引從0開始)。 – sawa 2011-05-10 19:13:09

+0

Ruby的'each'遍歷數組中的元素;你不必關心櫃檯。這個概念被稱爲內部迭代器,是紅寶石的一個特性。 'puts'爲當前元素添加一行結束並打印它。 – sawa 2011-05-10 19:15:20

1

是的,這是神祕的:)但是,如果這4個值總是數組的第4,你可以這樣做:現在

@query_result[0..3].each do |test| 
    puts test 
end 
+0

我認爲那些前4個只是索引名稱。 – GeekedOut 2011-05-10 18:45:05

+0

我更新了結果集,希望對我的問題更有意義。 – GeekedOut 2011-05-10 18:46:52

+0

@GeekedOut你想要對這些數據做什麼? – xinit 2011-05-10 18:59:26

1

測試與當前讀取的數據陣列。您可以使用

test[index] 

來獲取您的數據。

puts test[0] 

應該打印[「s」,「label」]。

由於第二陣列是鋸齒狀,您可以使用

puts test[1][index] 

得到的值的指數爲基礎的0項。

+0

其實這給了一個錯誤:「不能將字符串轉換爲整數」 – Genadinik 2011-05-10 19:08:24

+0

啊,這是一個實際的數組。爲你更新它。 – 2011-05-10 19:09:33