Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

parse values from a string

27 views
Skip to first unread message

gerberdata

unread,
Dec 23, 2011, 4:46:37 PM12/23/11
to
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

Marvin Gülker

unread,
Dec 23, 2011, 5:27:51 PM12/23/11
to
Have a look at String#split:

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

Vale,
Quintus

Robert Klemme

unread,
Dec 25, 2011, 9:17:17 AM12/25/11
to
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

gerberdata

unread,
Dec 27, 2011, 3:11:59 PM12/27/11
to
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",:official_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

Robert Klemme

unread,
Dec 28, 2011, 5:56:33 AM12/28/11
to

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",:official_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/

David Gerber

unread,
Dec 30, 2011, 2:04:16 PM12/30/11
to
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















Robert Klemme

unread,
Jan 1, 2012, 1:00:33 PM1/1/12
to
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

Willem Burbach

unread,
Jan 13, 2012, 1:34:54 PM1/13/12
to
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

unread,
Jan 13, 2012, 1:36:05 PM1/13/12
to
Op 12/23/2011 10:46 PM, gerberdata schreef:

Simon Krahnke

unread,
Jan 14, 2012, 2:13:42 PM1/14/12
to
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

unread,
Jan 18, 2012, 8:06:21 PM1/18/12
to

Thanks for all the suggestions!
On Jan 14, 11:13 am, Simon Krahnke <overl...@gmx.li> wrote:
0 new messages