ruby - How to convert data in a file to a nested hash or an array of hashes -


i have file in following format. have 100 records starting record 1 in file. here few records file:

record 1:  [record      acct_num      "11111111"     trxn_dt       "20140822"      post_dt       "20140822"     trxn_amt      "33.4"     trxn_cat      "rci"     trxn_cat_desc "abcd"]  record 2:  [record     acct_num      "44444444"     trxn_dt       "20140819"     post_dt       "20140822"     trxn_amt      "183.75"     trxn_cat      "aci"     trxn_cat_desc "defg"]   

i want convert data in file nested hash or array of hashes can perform assertions. want nested hash like:

{   {     acct_num => "11111111",     trxn_dt => "20140822",     post_dt => "20140822",     trxn_amt => "33.4",     trxn_cat => "rci",     trxn_cat_desc => "abcd"   },   {     acct_num => "44444444",     trxn_dt => "20140819",     post_dt => "20140822",     trxn_amt => "183.75",     trxn_cat => "aci",     trxn_cat_desc => "defg"   } } 

any pointers helpful.

my bad. didn't upload code have tried. letting me know. i'm new ruby , have tried far:

temp_file_output = file.open('file.txt', 'r')                                                        temp_file_output.readlines.each |line|                                                      if line.match(/record/)                                                                               new_line = line.next                                                  key, value = new_line.chomp.split(/"/)           file_read_hash[key] = value       end     end 

ok today tried revisit code , made below changes.i able write first element hash. still figuring out on how include remaining elements hash. pointers/suggestions? here's code:

file_read_hash = {}      temp_file_output = file.open('sample.txt', 'r')      while !temp_file_output.eof?         line = temp_file_output.readline         if line.include?("record")           new_line = temp_file_output.readline.next             key,value = new_line.chomp.split(/"/)             file_read_hash[key.strip] = value             break if line.include?("]")           end         end      puts file_read_hash   

output: {"rtr_acct_num"=>"44444444"}

file_read_hash = {}      file_read_hash_collect = {}      @file_read_array = []       file.foreach('sample.txt') |line|       next if line.include?("record") or line.empty?        next if line.include?("[record")         key,value = line.chomp.split(/"/)         key = key.to_s.strip         value = value.to_s.strip         file_read_hash[key] = value       file_read_hash_collect = file_read_hash.delete_if{|k,v|k.blank? ,  v.blank?}     if line.include?("]")         @file_read_array.push(file_read_hash_collect)         file_read_hash = {}         file_read_hash_collect = {}   end end puts @file_read_array 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -