Joel VanderWerf
4/14/2009 8:42:00 PM
Robert Dober wrote:
> x.split(/\s*(\w+):\s*/)[-8..-1].each_cons(2).map{ |k,v| "#{k} = #{v}" }
Scan is also useful for this kind of thing...
s = "ScriptName:changeTests Pass:15 Fail:0 Total:15"
p s.scan(/(\w+):(\S+)/)
# ==> [["ScriptName", "changeTests"], ["Pass", "15"], ["Fail", "0"],
["Total", "15"]]
To deal with the full string:
s = "send data StartTime: 20090413 13:41:53.000 ScriptName:changeTests
Pass:15 Fail:0 Total:15"
a = s.scan(/(\w+):\s*([\s\d:.]+|\S+)/)
p a
# ==> [["StartTime", "20090413 13:41:53.000 "], ["ScriptName",
"changeTests"], ["Pass", "15 "], ["Fail", "0 "], ["Total", "15"]]
a.shift
a.each {|key,val| puts "#{key} = #{val}"}
# ==>
# ScriptName = changeTests
# Pass = 15
# Fail = 0
# Total = 15
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407