Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to split dot “.” only before equal “=”

Reply
Thread Tools

How to split dot “.” only before equal “=”

 
 
Sira PS
Guest
Posts: n/a
 
      04-23-2011
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 is has a problem for the value
which comes after equal sign.

any ideas?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      04-23-2011
str = "Project.risksPotentialAfterSum=Pot. aft."

pieces = str.split(/=/)
puts pieces

--output:--
Project.risksPotentialAfterSum
Pot. aft.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Sira PS
Guest
Posts: n/a
 
      04-23-2011
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?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Roy Zuo
Guest
Posts: n/a
 
      04-23-2011
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:
> 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?
>
> --
> Posted via http://www.ruby-forum.com/.
>


--
__________________________________________________ __________________
/ It now costs more to amuse a child than it once did to educate his \
\ father. /
--------------------------------------------------------------------
\ ,__,
\ (oo)____
(__) )\
||--|| *

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-23-2011
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=Downloadin g.test'

pieces = str.split(/=/)
more_pieces = pieces[0].split(/[.]/)

data = more_pieces << pieces[1]

data.each_slice(2) do |arr|
k, v = arr

if v.nil?
puts "#{k} => nil"
end
end

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      04-24-2011
On Sat, Apr 23, 2011 at 7:38 PM, Roy Zuo <(E-Mail Removed)> 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

 
Reply With Quote
 
Sira PS
Guest
Posts: n/a
 
      04-24-2011
I already got the solution. thx all

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      04-24-2011
On Sun, Apr 24, 2011 at 9:33 AM, Robert Dober <(E-Mail Removed)> wrote:
> On Sat, Apr 23, 2011 at 7:38 PM, Roy Zuo <(E-Mail Removed)> 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.




--
The 1,000,000th fibonacci number contains '42' 2039 times; that is
almost 30 occurrences more than expected (208988 digits).
N.B. The 42nd fibonacci number does not contain '1000000' that is
almost the expected 3.0e-06 times.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-25-2011
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=Downloadin g.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_downloadi ng"=>"Downloading.test"}}}}

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-26-2011
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=Downloadin g.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_downloadi ng"=>"Downloading.test"}}}}

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Internet Sharing: Equal upload speeds but un-equal download speeds =?Utf-8?B?TkpU?= Wireless Networking 3 09-15-2007 06:22 AM
Dynamically resize visible text and append dot dot dot (like gmail subject lines.. ) how to question? jawolter@gmail.com Javascript 6 12-12-2005 04:09 PM
volatile struct in dot h vs dot c Christopher M. Lusardi C Programming 3 05-15-2004 12:17 AM
To dot or not to dot? Samul van Laere HTML 8 10-16-2003 02:55 PM



Advertisments