0

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://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?

8
  • 1
    Are you just trying to get the first four values of this array, or are you trying to do something else? Commented May 10, 2011 at 18:22
  • You have to mention what is @query_result and what do you want exactly. Commented May 10, 2011 at 18:29
  • @dogenpunk No, those are not the 4 values. Those are the indexes. I have a column that is called "label" and a column that is called "s" - I am just trying to extract the values for them. Commented May 10, 2011 at 18:31
  • So, you're getting back an array from this service and the first four values of this array are column headers? Are you looking to get something like an array of hashes out of this with the column headers as keys? Commented May 10, 2011 at 18:33
  • @GeekedOut It would be easier to see what's going on if you simply gave the output of puts @query_result.inspect so we can tell if it is a simple flat array, or an array of arrays, and what the elements are. And what service are you using? Is there documentation of the format? Commented May 10, 2011 at 18:41

3 Answers 3

4

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

  ...

]
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah the [1,1] did it. It is different from other lanuages where you have to keep incrementing the counter of the loop. Does the put take out the current array element and that is why [1,1] always points to the current value?
It's not [1, 1]. It's [1][1]. Its means the second item of the second item of the original array (Note that index starts from 0).
Ruby's each iterates over the elements within the array; you don't have to care about the counter. This concept is called internal iterator, a characteristics of ruby. puts adds a line end to the current element and prints it.
1

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

4 Comments

I think those first 4 are just the index names.
I updated the result set to hopefully make more sense in my question.
@GeekedOut What exactly do you want to do with this data?
I just want to get row values. Each row should have a value for s and label. And I want to be able to extract those.
1

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.

2 Comments

Actually that gave an error: "can't convert string to integer"
Ah, it's an actual array. Updated it for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.