Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3405 > unrolled thread

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

Started bySira PS <ploy.sukachai@gmail.com>
First post2011-04-23 12:05 -0500
Last post2011-04-24 03:02 -0500
Articles 10 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  How to split dot “.” only before equal  “=” Sira PS <ploy.sukachai@gmail.com> - 2011-04-23 12:05 -0500
    Re: How to split dot “.” only before  equal “=” 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-23 12:08 -0500
    Re: How to split dot “.” only before  equal “=” Sira PS <ploy.sukachai@gmail.com> - 2011-04-23 12:31 -0500
      Re: How to split dot “. ” only before equal “=” Roy Zuo <roylzuo@gmail.com> - 2011-04-23 12:38 -0500
        Re: How to split dot “.” only before equal “= ” Robert Dober <robert.dober@gmail.com> - 2011-04-24 02:33 -0500
          Re: How to split dot “.” only before equal “= ” Robert Dober <robert.dober@gmail.com> - 2011-04-24 03:09 -0500
      Re: How to split dot “.” only before  equal “=” 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-23 17:57 -0500
      Re: How to split dot “.” only before  equal “=” 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-25 16:27 -0500
        Re: How to split dot “.” only before  equal “=” 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-25 19:46 -0500
    Re: How to split dot “.” only before  equal “=” Sira PS <ploy.sukachai@gmail.com> - 2011-04-24 03:02 -0500

#3405 — How to split dot “.” only before equal “=”

FromSira PS <ploy.sukachai@gmail.com>
Date2011-04-23 12:05 -0500
SubjectHow to split dot “.” only before equal “=”
Message-ID<18aacfcb7224df848f220e5e5c060ee9@ruby-forum.com>
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/.

[toc] | [next] | [standalone]


#3406 — Re: How to split dot “.” only before equal “=”

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-23 12:08 -0500
SubjectRe: How to split dot “.” only before equal “=”
Message-ID<8b0ca5a79cc1103e06200b4ba04218ce@ruby-forum.com>
In reply to#3405
str = "Project.risksPotentialAfterSum=Pot. aft."

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

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

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

[toc] | [prev] | [next] | [standalone]


#3407 — Re: How to split dot “.” only before equal “=”

FromSira PS <ploy.sukachai@gmail.com>
Date2011-04-23 12:31 -0500
SubjectRe: How to split dot “.” only before equal “=”
Message-ID<2bcb1fea255cf99800791245bd83bfac@ruby-forum.com>
In reply to#3405
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/.

[toc] | [prev] | [next] | [standalone]


#3410 — Re: How to split dot “. ” only before equal “=”

FromRoy Zuo <roylzuo@gmail.com>
Date2011-04-23 12:38 -0500
SubjectRe: How to split dot “. ” only before equal “=”
Message-ID<20110423173737.GA27274@bender>
In reply to#3407
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)____
           (__)    )\
              ||--|| *

[toc] | [prev] | [next] | [standalone]


#3422 — Re: How to split dot “.” only before equal “= ”

FromRobert Dober <robert.dober@gmail.com>
Date2011-04-24 02:33 -0500
SubjectRe: How to split dot “.” only before equal “= ”
Message-ID<BANLkTikG-3+q2yVi2A9r61XQp5RUJMcEpg@mail.gmail.com>
In reply to#3410
On Sat, Apr 23, 2011 at 7:38 PM, Roy Zuo <roylzuo@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#3425 — Re: How to split dot “.” only before equal “= ”

FromRobert Dober <robert.dober@gmail.com>
Date2011-04-24 03:09 -0500
SubjectRe: How to split dot “.” only before equal “= ”
Message-ID<BANLkTik2KBGjkOEOiXTzr-AYPWKHNAB=eQ@mail.gmail.com>
In reply to#3422
On Sun, Apr 24, 2011 at 9:33 AM, Robert Dober <robert.dober@gmail.com> wrote:
> On Sat, Apr 23, 2011 at 7:38 PM, Roy Zuo <roylzuo@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#3417 — Re: How to split dot “.” only before equal “=”

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-23 17:57 -0500
SubjectRe: How to split dot “.” only before equal “=”
Message-ID<23edb8e53582735fd8939ea1a20e3189@ruby-forum.com>
In reply to#3407
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]

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

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

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

[toc] | [prev] | [next] | [standalone]


#3478 — Re: How to split dot “.” only before equal “=”

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-25 16:27 -0500
SubjectRe: How to split dot “.” only before equal “=”
Message-ID<767901e2edc9e860b36205e0197f978f@ruby-forum.com>
In reply to#3407
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"}}}}

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

[toc] | [prev] | [next] | [standalone]


#3485 — Re: How to split dot “.” only before equal “=”

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-25 19:46 -0500
SubjectRe: How to split dot “.” only before equal “=”
Message-ID<d9fe0ee748ddc101bcee7888c640c607@ruby-forum.com>
In reply to#3478
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"}}}}

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

[toc] | [prev] | [next] | [standalone]


#3423 — Re: How to split dot “.” only before equal “=”

FromSira PS <ploy.sukachai@gmail.com>
Date2011-04-24 03:02 -0500
SubjectRe: How to split dot “.” only before equal “=”
Message-ID<1a4d0c957e06cb0a4287e3cded81e1e4@ruby-forum.com>
In reply to#3405
I already got the solution. thx all

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web