File Contents into Hash Table?

Hello,

I’m trying put some file contents into a hash table. The file contains
something like these lines, of the format:

“…”=>65,
“…”=>66,
“[”=>67,
“]”=>68,
“[]=”=>69,
“<<”=>70,
“>>”=>71,
“-=”=>72,
“.”=>73,
“,”=>74,
“(”=>75,
“)”=>76,

Where each “something”=some_number, is on a separate line. I
basically need to put all these lines into a hash table. They’re already
in the hash format as you can see, with commas separating each entry and
everything. I can put them from the file into an array, using this:

file_contents = []
f = File.open(“RubyTokens.txt”) or die “Unable to open file…”
f.each_line { |line| file_contents.push(line)}

But I have no idea how to take each line and put it into a hash table.
Is there any way to just read the line in, and put the whole line in as
a hash key/value pair?

Update:

For example, I’m trying something like this:

hash_table = file_contents.each { |line| Hash[line.to_s] }

And it’s obviously giving me an error. With my limited programming
skills, and my terminally-ill Ruby skillset, this is proving rather
difficult. Thanks in advance for any help.

Hi, you can try parse lines with regexp as follows:

hash = {}
File.open(“RubyTokens.txt”) do |f|
f.each_line do |line|
line =~ /"([^"]+)"=>(\d+),/
hash[$1] = $2.to_i
end
end

2010/8/27 James R. [email protected]:

file_contents = []
f = File.open(“RubyTokens.txt”) or die “Unable to open file…”

That line is dysfunctional. Please kick this Perlism out of your Ruby
toolbox ASAP. If there is an error opening the file you’ll get an
exception and the default behavior is to exit the interpreter (unless
you catch the exception somewhere).

f.each_line { |line| file_contents.push(line)}

But I have no idea how to take each line and put it into a hash table.
Is there any way to just read the line in, and put the whole line in as
a hash key/value pair?

You can use the approach suggested with regular expression although I
would also check whether the regexp really matches.

h = {}
File.foreach “RubyTokens.txt” do |line|
h[$1] = $2.to_i if /^\s*"([^"])"\s=>\s*(\d+)\s*,\s*$/ =~ line
end

Note that I choose “*” for the quoted text - no idea whether the empty
string is allowed in your case. Also, I allowed for whitespace
inserted everywhere.

Kind regards

robert

And it shows all the keys, but it’s still not finding it when I do a
simple lookup:

puts hash[")"]

that is strange our both codes are working well with given example

Is there something simple I am missing?

can you post your whole code which doesn’t work?

Hello,

I tried your method, and debugging, it looks like all the key value
pairs get put in. However, I try to do this:

puts hash[")"]

And it returns “nil.” “)” is definitely in the text file, and definitely
in the hash table. But it not finding it. I did this:

hash.each_key { |key| puts key }

And it shows all the keys, but it’s still not finding it when I do a
simple lookup:

puts hash[")"]

Is there something simple I am missing?

pejuko wrote:

And it shows all the keys, but it’s still not finding it when I do a
simple lookup:

puts hash[")"]

that is strange our both codes are working well with given example

Is there something simple I am missing?

can you post your whole code which doesn’t work?

Ha, It’s all sorted now. There was something wrong with the charset of
my .txt file. I put it into a fresh .txt file and it sorted it out. Good
grief, sometimes it’s the random things that get you. Thanks again.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs