Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > parse values from a string

Reply
Thread Tools

parse values from a string

 
 
gerberdata
Guest
Posts: n/a
 
      12-23-2011
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

 
Reply With Quote
 
 
 
 
Marvin Gülker
Guest
Posts: n/a
 
      12-23-2011
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
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-25-2011
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

 
Reply With Quote
 
gerberdata
Guest
Posts: n/a
 
      12-27-2011
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",fficial_website=>"www.ge rberdata.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 <(E-Mail Removed)> wrote:
> On 12/23/2011 11:27 PM, Marvin Glker 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


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-28-2011

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",fficial_website=>"www.ge rberdata.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/
 
Reply With Quote
 
David Gerber
Guest
Posts: n/a
 
      12-30-2011
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__nickn ame =>
"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















 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-01-2012
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__nickn ame =>
> "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
 
Reply With Quote
 
Willem Burbach
Guest
Posts: n/a
 
      01-13-2012
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
 
Reply With Quote
 
Willem Burbach
Guest
Posts: n/a
 
      01-13-2012
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
 
Reply With Quote
 
Simon Krahnke
Guest
Posts: n/a
 
      01-14-2012
* Willem Burbach <(E-Mail Removed)> (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
parse a string of parameters and values bsneddon Python 7 12-15-2009 12:51 AM
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
Unable to parse multiple values using XMLStarlet kgould@yahoo.com XML 2 08-28-2004 03:18 PM
Long.parseLong() doesn't parse huge long values!!! JDK Bug?? Hans Java 12 08-14-2003 04:40 PM



Advertisments