How to split dot “.†only before equal “=â€

Discussion in 'Ruby' started by Sira PS, Apr 23, 2011.

  1. Sira PS

    Sira PS Guest

    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/.
    Sira PS, Apr 23, 2011
    #1
    1. Advertising

  2. Sira PS

    7stud -- Guest

    Re: How to split dot “.†only before equal “=â€

    str = "Project.risksPotentialAfterSum=Pot. aft."

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

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

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 23, 2011
    #2
    1. Advertising

  3. Sira PS

    Sira PS Guest

    Re: How to split dot “.†only before equal “=â€

    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/.
    Sira PS, Apr 23, 2011
    #3
  4. Sira PS

    Roy Zuo Guest

    Re: How to split dot “. †only before equal “=â€

    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)____
    (__) )\
    ||--|| *
    Roy Zuo, Apr 23, 2011
    #4
  5. Sira PS

    7stud -- Guest

    Re: How to split dot “.†only before equal “=â€

    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/.
    7stud --, Apr 23, 2011
    #5
  6. Sira PS

    Robert Dober Guest

    Re: How to split dot “.†only before equal “= â€

    On Sat, Apr 23, 2011 at 7:38 PM, Roy Zuo <> 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
    Robert Dober, Apr 24, 2011
    #6
  7. Sira PS

    Sira PS Guest

    Re: How to split dot “.†only before equal “=â€

    I already got the solution. thx all

    --
    Posted via http://www.ruby-forum.com/.
    Sira PS, Apr 24, 2011
    #7
  8. Sira PS

    Robert Dober Guest

    Re: How to split dot “.†only before equal “= â€

    On Sun, Apr 24, 2011 at 9:33 AM, Robert Dober <> wrote:
    > On Sat, Apr 23, 2011 at 7:38 PM, Roy Zuo <> 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.
    Robert Dober, Apr 24, 2011
    #8
  9. Sira PS

    7stud -- Guest

    Re: How to split dot “.†only before equal “=â€

    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/.
    7stud --, Apr 25, 2011
    #9
  10. Sira PS

    7stud -- Guest

    Re: How to split dot “.†only before equal “=â€

    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/.
    7stud --, Apr 26, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuël van Laere

    To dot or not to dot?

    Samuël van Laere, Oct 16, 2003, in forum: HTML
    Replies:
    8
    Views:
    423
    Samuël van Laere
    Oct 16, 2003
  2. Christopher M. Lusardi

    volatile struct in dot h vs dot c

    Christopher M. Lusardi, May 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    475
    Peter Shaggy Haywood
    May 15, 2004
  3. Nathan Sokalski
    Replies:
    11
    Views:
    703
    AAaron123
    Aug 14, 2009
  4. krishnan

    Dot Net Project Execution without Dot Net and Framework....

    krishnan, Jan 7, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    185
    krishnan
    Jan 7, 2006
  5. Replies:
    6
    Views:
    245
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page