bsteo
bsteo

Reputation: 1779

Print array element by name

I have this array:

[["RESULT", "1"], ["RESPMSG", "User authentication failed"]]

I want to access the RESPMSG element of the array and print its value, which can change, but its name is constant.

Any idea how I can print the array element by name? I tried like this, but I miss something:

decoded = URI::decode_www_form(@response_body, enc=Encoding::UTF_8)
respmsg = decoded.index("RESPMSG")
puts respmsg

The main problem is that RESPMSG is not always at the position 1 in the array, otherwise would be easier.

Upvotes: 0

Views: 60

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

You can use a hash structure to access the value for "RESPMSG"

respmsg = Hash[decoded]["RESPMSG"]

Upvotes: 4

Related Questions