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. Advertisements

  2. Have a look at String#split:

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

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

  3. Is this in a single string or in multiple strings? Can there be other
    values between underscores? Can you give a http://sscce.org/ ?
    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













     
    gerberdata, Dec 27, 2011
    #4
  5. Please do not top post.

    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
     
    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. I am glad you found a solution. Are you really sure you understand what
    you need and what your code does?
    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.
    Argument objects is nowhere used in the method.
    You only use the part after "__" so the part before is useless. Why
    pass it in and go through all the hoops of splitting?
    p_hash is a variable; variables do not "return" anything, only methods do.
    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:
    [
    "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:
    [
    "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. 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!
     
    gerberdata, Jan 19, 2012
    #11
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...