Replace in Ruby commas by different strings -



Replace in Ruby commas by different strings -

i have string called "example", this:

192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812 192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807

and obtain output:

{"string1":"192.168.1.40","string2":"8.8.8.8",“string3":“12.34.45.56”,“string4”:408,“string5”:“-”,"string6":1812} {"string1":"192.168.1.128","string2":"192.168.101.222",“string3":“12.34.45.56”,“string4”:384,“string5”:“-”,"string6":1807}

i did this:

example = example.gsub("\n","}\n{\"string1\": \"") illustration = example.insert(0, "{\"string1\": \"") illustration = example.concat("}")

and obtained:

{"string1":"192.168.1.40,8.8.8.8,12.34.45.56,408,-,1812} {"string1":"192.168.1.128,192.168.101.222,12.34.45.56,384,-,1807}

but don't know how can others changes. thanks!!

well, ruby hash, can output json or whatever need:

out = {} your_input_data.split(",").each_with_index { |val, i| out["string#{i}"] = val }

(but need each line: input.lines.each { |line| ... above here } - not clear - want list of maps?)

ruby string replace

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -