Hash table questions

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

···

On Sep 16, 2005, at 10:11 AM, EdUarDo wrote:

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?

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?

@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

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?

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

···

On Sep 16, 2005, at 10:31 AM, EdUarDo wrote:

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?

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?

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

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

James Edward Gray II

···

On Sep 16, 2005, at 10:26 AM, EdUarDo wrote:

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?

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

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.

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

James Edward Gray II

···

On Sep 16, 2005, at 10:46 AM, EdUarDo wrote:

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

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

James Edward Gray II

···

On Sep 16, 2005, at 10:51 AM, EdUarDo wrote:

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.

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.

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

···

On Sep 16, 2005, at 11:11 AM, EdUarDo wrote:

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

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!