parse values from a string

G

gerberdata

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
 
M

Marvin Gülker

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
 
R

Robert Klemme

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
 
G

gerberdata

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













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

Robert Klemme

Please do not top post.

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
 
D

David Gerber

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
 
R

Robert Klemme

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
 
W

Willem Burbach

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
 
W

Willem Burbach

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
 
S

Simon Krahnke

* Willem Burbach said:
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
 
G

gerberdata

Thanks for all the suggestions!
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

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top