I need to split dot only before equal to assign to hash
e.g.
Project.risksPotentialAfterSum=Pot. aft.
result after split should be like this:
{Project=>{risksPotentialAfterSum=>Pot. aft.}}
for now I use str.split(/[.=]/,2) which has a problem for the value
which comes after equal sign.
any ideas?
str = “Project.risksPotentialAfterSum=Pot. aft.”
str = “Project.risksPotentialAfterSum=Pot. aft.”
pieces = str.split(/=/)
puts pieces[0]
–output:–
Project.risksPotentialAfterSum
7stud,
actually i need to split dot before equal sign except after equal sign
the values i need are
Project
risksPotentialAfterSum
Pot. aft.
can i split it the get these result in the same time?
Sira PS wrote in post #994664:
7stud,
actually i need to split dot before equal sign except after equal sign
the values i need are
Project
risksPotentialAfterSum
Pot. aft.
can i split it the get these result in the same time?
str = ‘projects.risks.Index.flash_downloading=Downloading.test’
pieces = str.split(/=/)
more_pieces = pieces[0].split(/[.]/)
data = more_pieces << pieces[1]
p data
data.each_slice(2) do |arr|
k, v = arr
if v.nil?
puts “#{k} => nil”
end
end
–output:–
[“projects”, “risks”, “Index”, “flash_downloading”, “Downloading.test”]
Downloading.test => nil
On Sat, Apr 23, 2011 at 7:38 PM, Roy Z. [email protected] wrote:
You need regex lookahead syntax
ruby-1.9.2-head > str.split( /.(?=[^=]=)/ )
actually the lookahead can be a little simpler here, (?=.=) does the
trick, or did I miss any particular edge case you had in mind.
Please note that [^=]= can always be expressed as .??= and often,
that is unless backtracking can occur, as .*?=.
There is an edge case in which neither of our regexen might deliver
the required result, “a.b=c.d=e”, but as there is no spec, there is no
solution
Cheers
Robert
I already got the solution. thx all
You need regex lookahead syntax
ruby-1.9.2-head > str.split( /.(?=[^=]*=)/ )
=> [“Project”, “risksPotentialAfterSum=Pot. aft.”]
Roy
On Sun, Apr 24, 2011 at 02:31:22AM +0900, Sira PS wrote:
can i split it the get these result in the same time?
–
Posted via http://www.ruby-forum.com/.
–
/ It now costs more to amuse a child than it once did to educate his
\ father. /
\ ,__,
\ (oo)____
(__) )\
||--|| *
Sira PS wrote in post #994664:
7stud,
actually i need to split dot before equal sign except after equal sign
the values i need are
Project
risksPotentialAfterSum
Pot. aft.
can i split it the get these result in the same time?
That isn’t necessary:
str = ‘projects.risks.Index.flash_downloading=Downloading.test’
key_str, val = str.split(’=’)
keys = key_str.split(/[.]/)
temp = master = {}
last = keys.last
keys.each do |key|
if key == last
temp[key] = val
else
temp = temp[key] = {}
end
end
p master
–output:–
{“projects”=>{“risks”=>{“Index”=>{“flash_downloading”=>“Downloading.test”}}}}
On Sun, Apr 24, 2011 at 9:33 AM, Robert D. [email protected]
wrote:
On Sat, Apr 23, 2011 at 7:38 PM, Roy Z. [email protected] wrote:
You need regex lookahead syntax
ruby-1.9.2-head > str.split( /.(?=[^=]=)/ )
actually the lookahead can be a little simpler here, (?=.=) does the
trick, or did I miss any particular edge case you had in mind.
Sorry I git confused below
Please note that [^=]= can always be expressed as .??= and often,
With .?? I meant (?>.?)=, which is not really worth of replacing
[^=]= (but might come in handy if = were a complex subexpression). I
am not so sure about always either as there might be further context
which forces the .? part to be too greedy, maybe.
7stud – wrote in post #994963:
…and you can even get rid of that ugly if check every time through the
loop:
str = ‘projects.risks.Index.flash_downloading=Downloading.test’
key_str, val = str.split(’=’)
keys = key_str.split(/[.]/)
temp = master = {}
last = keys.pop
keys.each do |key|
temp = temp[key] = {}
end
temp[last] = val
p master
–output:–
{“projects”=>{“risks”=>{“Index”=>{“flash_downloading”=>“Downloading.test”}}}}