parse values from a string

Discussion in 'Ruby' started by gerberdata, Dec 23, 2011.

  1. gerberdata

    gerberdata Guest

    Wondering if someone can help me with this

    I have a string
    athlete_1_birthdate
    athlete_1_nickname
    athlete_1_first_name

    what I need to produce is

    athlete
    birthdate

    athlete
    nickname

    athlete
    first_name
     
    gerberdata, Dec 23, 2011
    #1
    1. Advertising

  2. On 23.12.2011 22:46, gerberdata wrote:
    > Wondering if someone can help me with this
    >
    > I have a string
    > athlete_1_birthdate
    > athlete_1_nickname
    > athlete_1_first_name
    >
    > what I need to produce is
    >
    > athlete
    > birthdate
    >
    > athlete
    > nickname
    >
    > athlete
    > first_name
    >


    Have a look at String#split:

    "athlete_1_birthdate".split("_") #=> ["athlete", "1", "birthdate"]

    Vale,
    Quintus
     
    Marvin Gülker, Dec 23, 2011
    #2
    1. Advertising

  3. On 12/23/2011 11:27 PM, Marvin Gülker wrote:
    > On 23.12.2011 22:46, gerberdata wrote:
    >> Wondering if someone can help me with this
    >>
    >> I have a string
    >> athlete_1_birthdate
    >> athlete_1_nickname
    >> athlete_1_first_name


    Is this in a single string or in multiple strings? Can there be other
    values between underscores? Can you give a http://sscce.org/ ?

    >> what I need to produce is
    >>
    >> athlete
    >> birthdate
    >>
    >> athlete
    >> nickname
    >>
    >> athlete
    >> first_name
    >>

    >
    > Have a look at String#split:
    >
    > "athlete_1_birthdate".split("_") #=> ["athlete", "1", "birthdate"]


    Might work, might not work. The requirements are far too unclear to me.

    Kind regards

    robert
     
    Robert Klemme, Dec 25, 2011
    #3
  4. gerberdata

    gerberdata Guest

    Sorry that I did not give you clear enough requirements. Here is an
    example of what I am trying to do
    Athlete.create:)first_name => "Kobe",:last_name=>"Bryant", :birthdate
    =>"10/12/1969",:nickname=>"dave",:eek:fficial_website=>"www.gerberdata.net")

    {:athlete_nickname => "gerbdla",:athlete_birthdate =>
    "1/1/2010",:athlete_official_website => "www.gerberdata.net"}


    now I have this paramater set each params coresponds to a field in my
    database

    so :athlete_official_website should match
    athlete => object
    official_website => official_website
    :athlete_birthdate
    athlete
    birthdate
    :athlete_nickname
    athlete
    nickname

    so I need to account for two conditions
    1. sometimes the field has an underscore in it as official_website
    2. sometimes the field has no underscore so it is just birthdate

    or if there is a way to do this type of logic better that would be
    appreciated.

    Thanks

    David













    On Dec 25, 6:17 am, Robert Klemme <> wrote:
    > On 12/23/2011 11:27 PM, Marvin Gülker wrote:
    >
    > > On 23.12.2011 22:46, gerberdata wrote:
    > >> Wondering if someone can help me with this

    >
    > >> I have a string
    > >> athlete_1_birthdate
    > >> athlete_1_nickname
    > >> athlete_1_first_name

    >
    > Is this in a single string or in multiple strings?  Can there be other
    > values between underscores?  Can you give ahttp://sscce.org/?
    >
    > >> what I need to produce is

    >
    > >> athlete
    > >> birthdate

    >
    > >> athlete
    > >> nickname

    >
    > >> athlete
    > >> first_name

    >
    > > Have a look at String#split:

    >
    > > "athlete_1_birthdate".split("_") #=>  ["athlete", "1", "birthdate"]

    >
    > Might work, might not work.  The requirements are far too unclear to me..
    >
    > Kind regards
    >
    >         robert
     
    gerberdata, Dec 27, 2011
    #4
  5. Please do not top post.

    On 27.12.2011 21:11, gerberdata wrote:
    > Sorry that I did not give you clear enough requirements. Here is an
    > example of what I am trying to do
    > Athlete.create:)first_name => "Kobe",:last_name=>"Bryant", :birthdate
    > =>"10/12/1969",:nickname=>"dave",:eek:fficial_website=>"www.gerberdata.net")
    >
    > {:athlete_nickname => "gerbdla",:athlete_birthdate =>
    > "1/1/2010",:athlete_official_website => "www.gerberdata.net"}
    >
    >
    > now I have this paramater set each params coresponds to a field in my
    > database
    >
    > so :athlete_official_website should match
    > athlete => object
    > official_website => official_website
    > :athlete_birthdate
    > athlete
    > birthdate
    > :athlete_nickname
    > athlete
    > nickname
    >
    > so I need to account for two conditions
    > 1. sometimes the field has an underscore in it as official_website
    > 2. sometimes the field has no underscore so it is just birthdate
    >
    > or if there is a way to do this type of logic better that would be
    > appreciated.


    What logic? What is your input? What is your desired output? Do you
    want to split some strings into two and some not? According to what
    criteria? Or do you only want to extract one part? This is still
    unclear to me.

    Regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Dec 28, 2011
    #5
  6. gerberdata

    David Gerber Guest

    I figured out what I need to do unless you have a better way to do
    this

    This is the input. The first two elements are objects. The second
    params hash is what needs to be parsed
    athlete1.merge!(athlete2,athlete3,{:athlete__nickname =>
    "gerbdla",:athlete__birthdate => "1/1/2010",:athlete__official_website
    => "www.gerberdata.net"})

    here it the method

    def merge!(objects*,p_hash)
    p_hash.each do |k,v|
    obj = k.to_s.split("__")
    field = obj[1]
    self[field] = v

    end
    save!
    end

    this will return
    p_hash returns
    obj = athlete
    field = athlete_birthdate
    obj = athlete
    field = official_website

    so basically I am splitting the object and the field name. since the
    field name can contain underscores or not like birthdate or
    official_website
    I decided to use __ to split the object from the field.



    Regards

    David
     
    David Gerber, Dec 30, 2011
    #6
  7. On 12/30/2011 08:04 PM, David Gerber wrote:
    > I figured out what I need to do unless you have a better way to do
    > this


    I am glad you found a solution. Are you really sure you understand what
    you need and what your code does?

    > This is the input. The first two elements are objects. The second
    > params hash is what needs to be parsed
    > athlete1.merge!(athlete2,athlete3,{:athlete__nickname =>
    > "gerbdla",:athlete__birthdate => "1/1/2010",:athlete__official_website
    > => "www.gerberdata.net"})
    >
    > here it the method


    Defined in which class? In case you define this in class Hash please
    not that it does have a method #merge! already - with different argument
    lists and semantics. It's a bad idea to change a standard method in
    such a way.

    > def merge!(objects*,p_hash)


    Argument objects is nowhere used in the method.

    > p_hash.each do |k,v|
    > obj = k.to_s.split("__")
    > field = obj[1]


    You only use the part after "__" so the part before is useless. Why
    pass it in and go through all the hoops of splitting?

    > self[field] = v
    >
    > end
    > save!
    > end
    >
    > this will return
    > p_hash returns


    p_hash is a variable; variables do not "return" anything, only methods do.

    > obj = athlete
    > field = athlete_birthdate
    > obj = athlete
    > field = official_website
    >
    > so basically I am splitting the object and the field name. since the
    > field name can contain underscores or not like birthdate or
    > official_website
    > I decided to use __ to split the object from the field.


    I can only repeat myself: it's still unclear to me what you want and need.

    Good luck!

    robert
     
    Robert Klemme, Jan 1, 2012
    #7
  8. Op 12/23/2011 10:46 PM, gerberdata schreef:
    > Wondering if someone can help me with this
    >
    > I have a string
    > athlete_1_birthdate
    > athlete_1_nickname
    > athlete_1_first_name
    >
    > what I need to produce is
    >
    > athlete
    > birthdate
    >
    > athlete
    > nickname
    >
    > athlete
    > first_name
    >


    [
    "athlete_1_birthdate",
    "athlete_1_nickname",
    "athlete_1_first_name",
    ].each do |s|
    tokens = s.split(/_/)
    puts "#{tokens[0]}\n#{tokens[2..-1].join('_')}\n\n"
    end
     
    Willem Burbach, Jan 13, 2012
    #8
  9. Op 12/23/2011 10:46 PM, gerberdata schreef:
    > Wondering if someone can help me with this
    >
    > I have a string
    > athlete_1_birthdate
    > athlete_1_nickname
    > athlete_1_first_name
    >
    > what I need to produce is
    >
    > athlete
    > birthdate
    >
    > athlete
    > nickname
    >
    > athlete
    > first_name
    >


    [
    "athlete_1_birthdate",
    "athlete_1_nickname",
    "athlete_1_first_name",
    ].each do |s|
    tokens = s.split(/_/)
    puts "#{tokens[0]}\n#{tokens[2..-1].join('_')}\n\n"
    end
     
    Willem Burbach, Jan 13, 2012
    #9
  10. * Willem Burbach <> (2012-01-13) schrieb:

    >Op 12/23/2011 10:46 PM, gerberdata schreef:
    >> Wondering if someone can help me with this
    >>
    >> I have a string
    >> athlete_1_birthdate
    >> athlete_1_nickname
    >> athlete_1_first_name
    >>
    >> what I need to produce is
    >>
    >> athlete
    >> birthdate
    >>
    >> athlete
    >> nickname
    >>
    >> athlete
    >> first_name
    >>

    >
    > [
    > "athlete_1_birthdate",
    > "athlete_1_nickname",
    > "athlete_1_first_name",
    > ].each do |s|
    > tokens = s.split(/_/)
    > puts "#{tokens[0]}\n#{tokens[2..-1].join('_')}\n\n"
    > end


    No excessive splitting and rejoining:

    [
    "athlete_1_birthdate",
    "athlete_1_nickname",
    "athlete_1_first_name",
    ].each do |s|
    tokens = s.split(/_/, 3)
    puts "#{tokens[0]}\n#{tokens[2]}\n\n"
    end

    mfg, simon .... l
     
    Simon Krahnke, Jan 14, 2012
    #10
  11. gerberdata

    gerberdata Guest

    Thanks for all the suggestions!
    On Jan 14, 11:13 am, Simon Krahnke <> wrote:
    > * Willem Burbach <> (2012-01-13) schrieb:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > >Op 12/23/2011 10:46 PM, gerberdata schreef:
    > >> Wondering if someone can help me with this

    >
    > >> I have a string
    > >> athlete_1_birthdate
    > >> athlete_1_nickname
    > >> athlete_1_first_name

    >
    > >> what I need to produce is

    >
    > >> athlete
    > >> birthdate

    >
    > >> athlete
    > >> nickname

    >
    > >> athlete
    > >> first_name

    >
    > > [
    > >   "athlete_1_birthdate",
    > >   "athlete_1_nickname",
    > >   "athlete_1_first_name",
    > > ].each do |s|
    > >   tokens = s.split(/_/)
    > >   puts "#{tokens[0]}\n#{tokens[2..-1].join('_')}\n\n"
    > > end

    >
    > No excessive splitting and rejoining:
    >
    > [
    >   "athlete_1_birthdate",
    >   "athlete_1_nickname",
    >   "athlete_1_first_name",
    > ].each do |s|
    >   tokens = s.split(/_/, 3)
    >   puts "#{tokens[0]}\n#{tokens[2]}\n\n"
    > end
    >
    > mfg,                         simon .... l
     
    gerberdata, Jan 19, 2012
    #11
    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. Hans
    Replies:
    12
    Views:
    11,387
    Chris Smith
    Aug 14, 2003
  2. Replies:
    2
    Views:
    1,462
    Mikhail Grouchinsky
    Aug 28, 2004
  3. Replies:
    19
    Views:
    1,169
    Daniel Vallstrom
    Mar 15, 2005
  4. bsneddon
    Replies:
    7
    Views:
    370
    Tim Chase
    Dec 15, 2009
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    206
    7stud --
    Feb 20, 2008
Loading...

Share This Page