Hash table questions

E

EdUarDo

Hi all,

I'm filling up a hash table reading data from a file.

coords = {}
File.open('/home/eyp/socceralive/lib/model/zones.dat') do |file|
while !file.eof? do
pos = file.readline
line = file.readline
data = line.split(';')
coords.store(pos, {:CENTER=>data[0], :RIGHT=>data[1], :LEFT=>data[2]})
end
end

The content oh the hash table is:

coords.each do |key, value|
puts key, value
end

DC
CENTER2,8,6,10RIGHT0,8,3,10LEFT5,8,8,10
MP
CENTER3,5,7,8RIGHT5,5,8,8LEFT0,5,3,8
M
CENTER2,3,6,7RIGHT5,3,8,7LEFT0,3,3,7
DF
CENTER2,0,6,4RIGHT6,0,8,4LEFT0,0,2,4
CD
CENTER2,2,6,5RIGHT1,2,5,5LEFT3,2,7,5

So I suppose hash table has a 'DC' key, but these sentences return 'false'

@position.post = :DC
puts coords.has_key?(@position.post.to_s)
puts coords.has_key?(@position.post)
puts coords.has_key?('DC')

Why I can't access 'DC' key with @position.post?
 
J

James Edward Gray II

Hi all,

I'm filling up a hash table reading data from a file.

coords = {}
File.open('/home/eyp/socceralive/lib/model/zones.dat') do |file|
while !file.eof? do
pos = file.readline
line = file.readline
data = line.split(';')
coords.store(pos, {:CENTER=>data[0], :RIGHT=>data
[1], :LEFT=>data[2]})
end
end

The content oh the hash table is:

coords.each do |key, value|
puts key, value
end

DC
CENTER2,8,6,10RIGHT0,8,3,10LEFT5,8,8,10
MP
CENTER3,5,7,8RIGHT5,5,8,8LEFT0,5,3,8
M
CENTER2,3,6,7RIGHT5,3,8,7LEFT0,3,3,7
DF
CENTER2,0,6,4RIGHT6,0,8,4LEFT0,0,2,4
CD
CENTER2,2,6,5RIGHT1,2,5,5LEFT3,2,7,5

So I suppose hash table has a 'DC' key, but these sentences return
'false'

@position.post = :DC
puts coords.has_key?(@position.post.to_s)
puts coords.has_key?(@position.post)
puts coords.has_key?('DC')

Why I can't access 'DC' key with @position.post?

I suspect it's because the DC key had trailing whitespace, probably a
newline character. Try changing the reads to something like:

pos = file.readline.strip
line = file.readline.strip

Hope that helps.

James Edward Gray II
 
E

EdUarDo

@position.post = :DC
I suspect it's because the DC key had trailing whitespace, probably a
newline character. Try changing the reads to something like:

pos = file.readline.strip
line = file.readline.strip

It works fine, thank you.

Now I get a true answer when I ask:

puts coords.has_key?(@position.post.to_s)
puts coords.has_key?('DC')

but not when

puts coords.has_key?(@position.post)

is this correct? Do I need to do .to_s to retrieve data from hash?
 
E

EdUarDo

coords = {}
File.open('/home/eyp/socceralive/lib/model/zones.dat') do |file|
while !file.eof? do
pos = file.readline
line = file.readline
data = line.split(';')
coords.store(pos, {:CENTER=>data[0], :RIGHT=>data[1],
:LEFT=>data[2]})
end
end

More questions about this code. I'm filling up the hash with another hash tables.

If I access coords['DC'] I get another hash, but coords['DC']['CENTER'] retrieves me a nil value.
How must I access data from hash tables inside first hash table?
 
J

James Edward Gray II

It works fine, thank you.

Now I get a true answer when I ask:

puts coords.has_key?(@position.post.to_s)
puts coords.has_key?('DC')

but not when

puts coords.has_key?(@position.post)

is this correct? Do I need to do .to_s to retrieve data from hash?

If the key is a String, yes. If the key were a symbol, you could
omit it.

James Edward Gray II
 
J

James Edward Gray II

coords = {}
File.open('/home/eyp/socceralive/lib/model/zones.dat') do |file|
while !file.eof? do
pos = file.readline
line = file.readline
data = line.split(';')
coords.store(pos, {:CENTER=>data[0], :RIGHT=>data
[1], :LEFT=>data[2]})
end
end

More questions about this code. I'm filling up the hash with
another hash tables.

If I access coords['DC'] I get another hash, but coords['DC']
['CENTER'] retrieves me a nil value.
How must I access data from hash tables inside first hash table?

You used symbols for keys to the second hash:

coords["DC"][:CENTER]

Strings are not the same as symbols, or vice versa.

James Edward Gray II
 
E

EdUarDo

is this correct? Do I need to do .to_s to retrieve data from hash?
If the key is a String, yes. If the key were a symbol, you could omit it.

Key is a symbol, but it doesn't work if I not call to_s
 
1

1337p337

If I access coords['DC'] I get another hash, but coords['DC']['CENTER'] r=
etrieves me a nil value.
How must I access data from hash tables inside first hash table?
[James already answered, so I'm going to go half off-topic.]
IRB is infinitely useful for things like this. You can see how the
data looks and poke at it interactively instead of writing all the
code up front and wondering. 'puts coords.inspect' is also nice for
debugging.
 
E

EdUarDo

Key is a symbol, but it doesn't work if I not call to_s

Well, I got it, I'm not going to use symbols how keys, I'll use Strings because
then I'll can use atributes which are symbols to access values using to_s method.
 
J

James Edward Gray II

Key is a symbol, but it doesn't work if I not call to_s

The key (loaded into the Hash from the file) was a String, not a
Symbol, in the code you showed.

James Edward Gray II
 
J

James Edward Gray II

Well, I got it, I'm not going to use symbols how keys, I'll use
Strings because
then I'll can use atributes which are symbols to access values
using to_s method.

My opinion is that you should use the same thing at both ends and
skip the extra method called.

James Edward Gray II
 
E

EdUarDo

My opinion is that you should use the same thing at both ends and skip
the extra method called.

But file is readed only to get a configuration info, at the rest of code I
use symbols to reference that values. If there was a way to read symbols all
would be easy but I know it has no sense.
 
J

James Edward Gray II

If there was a way to read symbols all would be easy...

No problem:

coords.store(pos.to_sym, {:CENTER=>data[0], :RIGHT=>data
[1], :LEFT=>data[2]})

That should let you use Symbols for both Hashes.

James Edward Gray II
 
E

EdUarDo

coords.store(pos.to_sym, {:CENTER=>data[0], :RIGHT=>data [1],
:LEFT=>data[2]})

That should let you use Symbols for both Hashes.


8-| ooh, I didn't know to_sym method, snif!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top