File Contents into Hash Table?

J

James Rasmussen

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?
 
J

James Rasmussen

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

pejuko

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
 
R

Robert Klemme

2010/8/27 James Rasmussen said:
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
 
J

James Rasmussen

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?
 
P

pejuko

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?
 
J

James Rasmussen

pejuko said:
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.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top