简体   繁体   中英

Replace in Ruby commas by different strings

I have a string called "example", like 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 I would like to obtain this 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\": \"")                         
example = example.insert(0, "{\"string1\": \"")                  
example = example.concat("}")                                    

and I 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 I don't know how can I do the others changes. Thanks!!

Well, to get it as a ruby hash, which you can output as json or whatever you need:

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

(but you would need to do this for each line: input.lines.each { |line| ... do the above here } - but I am not clear - do you want a list of maps?)

I made the assumption that you didn't want values that were just numbers to be double-quoted.

DATA.each_line do |line|
    l = line.chomp.split(',').map.with_index do |v, i|
        v = v =~ /^\d+$/ ? v : "\"#{v}\""
        "\"string#{i+1}\":#{v}"
    end
    print "{", l.join(','), "}\n"
end

__END__
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

Result:

{"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}

It seems from the code you wrote that you are looking for a single string as output rather than a more elaborate Ruby data structure or output to a printed stream.

This is working for me:

example = '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'

result = example.split("\n").map do |line| 
  n = 0 
  line.split(',').map{|s| %Q|"string#{n+=1}":"#{s}"|}.join(',')
end.map{|c| "{#{c}}"}.join("\n")

puts result

{"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"}

This splits into lines then splits each line into separate strings, then concatenates each string with its JSON key and finally reassembles with join first with commas and then with newline. If you'd rathet have lists than reassembled strings, just omit the respective join.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM