开发者

Parsing a Ruby Array

I am new to Ruby as probably everyone here knows by now :) I have a query to some service and I get back an array. When I run this code

@query_result.each do |test|
  puts test
end

I get exactly this output

["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:/开发者_JS百科/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>"]]]

I know it is cryptic, but basically I just need to extract the values in these:

names
s
label
values

What would be the code to get the actual values of the columns from the array?


Your @query_result has the structure:

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

where array consists of pairs. I don't see anything useful from the literal strings "names", "s", "label", and "values". You probably want to take out array.

If you do

@query_result[1]

this will give you the second element of @query_result, which is

["values", array]

If you further do

@query_result[1][1]

This will give you the array part:

[
  [
    "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"
  ],

  ...

]


Yes, it's cryptic :) But if those 4 values are always the first 4 of the array, you could do something like:

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


Test is now an array with the current fetched row. You can use

test[index]

to fetch your data.

puts test[0]

should print ["s", "label"].

As the second array is jagged, you can use

puts test[1][index]

to get the 0-based entry at index of values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜