2011-05-10 6 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

+0

당신은 @query_result가 무엇인지 언급해야하며 정확히 무엇을 원하니? – shajin

+0

@dogenpunk 아니요, 4 가지 값이 아닙니다. 그것들이 지표입니다. 나는 "레이블"이라고 불리는 열과 "s"라고 불리는 열을 가지고있다 - 나는 그 열의 값을 추출하려고하고있다. – GeekedOut

답변

4

귀하의 @query_result는 구조를 가지고있다. 나는 리터럴 문자열 "names", "s", "label""values"에서 유용한 것을 보지 못했습니다. 아마도 array을 꺼내고 싶을 것입니다.

이 더

@query_result[1][1] 

이 당신에게 array을 줄 것이다 할 경우

@query_result[1] 

할 경우 이것은 당신에게

["values", array] 

이다 @query_result의 두 번째 요소를 줄 것이다 부 :

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

+0

그것은 [1, 1]이 아닙니다. 그것은'[1] [1]'입니다. 그것은 원래 배열의 두 번째 항목의 두 번째 항목을 의미합니다 (인덱스는 0부터 시작합니다). – sawa

+0

루비의'each'는 배열 내의 요소를 반복합니다; 카운터에 대해 신경 쓸 필요가 없습니다. 이 개념을 루비의 특성 인 내부 반복자라고합니다. 'puts'는 현재 요소에 줄 끝을 추가하고 그것을 출력합니다. – sawa

1

예, 그것은 비밀입니다 :)하지만 그 4 개 개의 값은 항상 배열의 4 첫 번째 경우에, 당신처럼 뭔가를 할 수 :

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

처음 4 개는 색인 이름 일 뿐이라고 생각합니다. – GeekedOut

+0

나는 내 질문에 더 많은 의미가 있기를 바랍니다. – GeekedOut

+0

@ GeekedOut이 데이터로 정확히 무엇을하고 싶습니까? – xinit

1

테스트 지금 현재 인출 된 행과 배열입니다.

test[index] 

을 사용하여 데이터를 가져올 수 있습니다.

puts test[0] 

[[s ","label "]을 인쇄해야합니다. 두 번째 배열이 고르지이므로

, 당신은 값의 인덱스에서 0 기반의 항목을 얻을 수

puts test[1][index] 

를 사용할 수 있습니다. array가 쌍으로 구성

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

:

+0

사실 그것은 오류가 발생했습니다 : "문자열을 정수로 변환 할 수 없습니다" – Genadinik

+0

아, 실제 배열입니다. 그것을 당신을 위해 업데이트했습니다. –