Unless var[3] == ''VALUE'' then
SourceHash[''VALUE''] is going to return a
nil, and your puts expression amounts to nil[''cktid''], which
would
produce the error you describe.
Also--in ruby, variables that begin w/capital letters are constants--you
should rename to source_hash or something in order to decrease your risk
of confusing the interpreter.
HTH,
-Roy
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of
Me
Sent: Tuesday, July 22, 2008 8:28 AM
To: Ruby on Rails: Talk
Subject: [Rails] File.open.each() while using a 2 level hash
I am doing:
SourceHash = {}
File.open("file_to_open").each {|line|
SourceHash[var[3]] = {}
puts SourceHash[''VALUE''][''cktid'']
}
var is just an array of each csv value.
The error says undefined method [] for nil:NilClass in ''each''
If I take the hash statement out of the file loop it works. Any ideas
why the "each" method is throwing an error while trying to get a value
out of a 2 level hash??
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---