hash code reference - this only returns 'nil' please help me

M

Mmcolli00 Mom

Please tell me what I am doing wrong. i thought h[ErrFieldID] would work
however it doen't. Thanks. Mom

h = {'25127' => 'RefProvNPI', '10353' => 'ProcCode'}


it ("Search for FieldId Tag containing error in XML file") do
ErrFieldID= get_xml_attrib_value(ie, "Error","FieldID")
puts ErrFieldID #This correctly shows 25127
puts h[ErrFieldID] # should show 'RefProvNPI' but doesn't
end
 
S

Sebastian Hungerecker

Mmcolli00 said:
=C2=A0 puts ErrFieldID =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 #This co=
rrectly shows 25127

Try p ErrFieldID
Maybe ErrFieldID is not a string or contains spaces or non-printable=20
characters somewhere.

HTH,
Sebastian
=2D-=20
NP: Metallica - Master of Puppets
Jabber: (e-mail address removed)
ICQ: 205544826
 
M

Mmcolli00 Mom

Sebastian said:
Try p ErrFieldID
Maybe ErrFieldID is not a string or contains spaces or non-printable
characters somewhere.

HTH,
Sebastian

No that didn't work. It only showed the reference key in brackets:
['25127']. I need it to show the value from the hash: 'RefProvNPI'

Thanks for trying.
Mom
 
M

Michael Libby

No that didn't work. It only showed the reference key in brackets:
['25127']. I need it to show the value from the hash: 'RefProvNPI'

It did work. It means that ErrFieldID is an array containing a single
string. Not the same as your hash key, which is just a string.

-Michael
 
R

reuben doetsch

[Note: parts of this message were removed to make it a legal post.]

Check the API of whatever gem you are using and check to see if
get_xml_attrib_value returns a single value or an array.

Reuben

No that didn't work. It only showed the reference key in brackets:
['25127']. I need it to show the value from the hash: 'RefProvNPI'

It did work. It means that ErrFieldID is an array containing a single
string. Not the same as your hash key, which is just a string.

-Michael
 
T

Todd Benson

Sebastian said:
Try p ErrFieldID
Maybe ErrFieldID is not a string or contains spaces or non-printable
characters somewhere.

HTH,
Sebastian

No that didn't work. It only showed the reference key in brackets:
['25127']. I need it to show the value from the hash: 'RefProvNPI'

Try puts ErrFieldID.class to tell you what it really is. Looks like
it is an Array object instead of a string.

Todd
 
M

Mmcolli00 Mom

It continues to show the wrong value ['25127']. I need it to show the
value from the hash: 'RefProvNPI'. The ErrFieldID.class returned
'Array'.

I don't know what else to try so that it will output the value
'RefProvNPI' from the hash. Having a hash will work perfectly for the
program however if I can't reference the value that the key points to, I
don't know what else to try. Thanks for helping.
 
D

David A. Black

Hi --

It continues to show the wrong value ['25127']. I need it to show the
value from the hash: 'RefProvNPI'. The ErrFieldID.class returned
'Array'.

I don't know what else to try so that it will output the value
'RefProvNPI' from the hash. Having a hash will work perfectly for the
program however if I can't reference the value that the key points to, I
don't know what else to try. Thanks for helping.

If it's an array and you need the first element, how about indexing it
with [0]? Or use the * operator. Also, make sure that you don't mix
'25127' (the string) with 25127 (the integer). As hash keys they are
different from each other.


David
 
M

Mmcolli00 Mom

Thanks everyone!

I thought about this being an array and I did this:

StrgErrFieldID = ErrFieldID.to_s

Then I referenced h[StrgErrFieldID]
This worked!

Thanks for all your help and support!
Mom
 
M

Martin DeMello

Thanks everyone!

I thought about this being an array and I did this:

StrgErrFieldID = ErrFieldID.to_s

this would be better:

StrgErrFieldID = ErrFieldID[0]

Since it explicitly says (i) that you're expecting an array and (ii)
that you're extracting its first element

martin
 
M

Michael Libby

Thanks everyone!

I thought about this being an array and I did this:

StrgErrFieldID = ErrFieldID.to_s

Then I referenced h[StrgErrFieldID]
This worked!

That will work as long as you can guarantee only one element in the
Array. The fact that the method returns an array and not a single
value indicates to me that there may be times where multiple elements
are a real possibility. You may want to consider accessing the first
element explicitly with ErrFieldID[0] instead of using to_s.

You may also want to consider what to do if you get more than one
element returned in that array.

-Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top