I have a text document that has formatted data in it, and I am attempted to determine the best way to parse the data to break it up into hashes that I can then write to my databases. I suppose my issue is determining where to tell Ruby to STOP reading in data to write into the database. The file would be similar to this: User: Username Phone: Phone number Address: Address Comments: Comments Data1: Data Data2: Data Data3: Data User: Username Phone: Phone number Address: Address Comments: Comments Data1: Data Data2: Data User: Username Phone: Phone number Address: Address Comments: Comments Data1: Data Data2: Data It''s not that difficult to chomp the \n and use a split to create a hash in a loop, but how do tell Ruby to start a new database write process if it hits a new "user" key value? Should I write a case statement to check each line for a "user" key and use that line to call a new instance of my database class/method? Is there a faster or more efficient way? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pardee, Roy
2008-Sep-11 18:45 UTC
Re: Beginner Question: Formatted Text Data -> Hash -> Database.
I''ll be interested to see what responses you get, but I can''t think of a more efficient way. I''d try something like: my_file.each_line do |line| if line ~= /$User/ and this_user.username.length > 0 then if this_user.save then puts("Saved user #{this_user.username}") else puts("Problem saving #{this_user.username}!:\n#{this_user.errors}") end this_user = User.new end attribute, val = line.split(/:\s*/) this_user.send(attribute.lower, val) end That''s air-code obviously--may be wildly off... -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Jisho Sent: Thursday, September 11, 2008 10:50 AM To: Ruby on Rails: Talk Subject: [Rails] Beginner Question: Formatted Text Data -> Hash -> Database. I have a text document that has formatted data in it, and I am attempted to determine the best way to parse the data to break it up into hashes that I can then write to my databases. I suppose my issue is determining where to tell Ruby to STOP reading in data to write into the database. The file would be similar to this: User: Username Phone: Phone number Address: Address Comments: Comments Data1: Data Data2: Data Data3: Data User: Username Phone: Phone number Address: Address Comments: Comments Data1: Data Data2: Data User: Username Phone: Phone number Address: Address Comments: Comments Data1: Data Data2: Data It''s not that difficult to chomp the \n and use a split to create a hash in a loop, but how do tell Ruby to start a new database write process if it hits a new "user" key value? Should I write a case statement to check each line for a "user" key and use that line to call a new instance of my database class/method? Is there a faster or more efficient way? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---