Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > splitting string to hash

Reply
Thread Tools

splitting string to hash

 
 
Brian Nice
Guest
Posts: n/a
 
      08-31-2006
I have a strings like the following:
s1- "[1] Hello [2] bye"
s2- "[1] Hello [2] bye [2:1] continue [2] more"

I want to convert them to hashes like
h1- {1 => "Hello", 2 => "bye"}
h2- {1 => "Hello", 2 => "bye", "2:1" => "continue", "2:2" => more"}

Is there an easy Ruby way to do this?
Thanks for the help
Brian

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      08-31-2006
On Aug 31, 2006, at 9:38 AM, Brian Nice wrote:

> s2- "[1] Hello [2] bye [2:1] continue [2] more"


> h2- {1 => "Hello", 2 => "bye", "2:1" => "continue", "2:2" => more"}


I assume more is missing a quote there.

Also, how was the 2:2 key determined?

James Edward Gray II

 
Reply With Quote
 
 
 
 
William Crawford
Guest
Posts: n/a
 
      08-31-2006
Brian Nice wrote:
> I have a strings like the following:
> s1- "[1] Hello [2] bye"
> s2- "[1] Hello [2] bye [2:1] continue [2] more"
>
> I want to convert them to hashes like
> h1- {1 => "Hello", 2 => "bye"}
> h2- {1 => "Hello", 2 => "bye", "2:1" => "continue", "2:2" => more"}
>
> Is there an easy Ruby way to do this?
> Thanks for the help
> Brian


Assuming that James is correct in that :2 is missing from the second
string, this works:

h2 = Hash[*s2.scan(/\[([^\[\]]+)\] (\w+)/).flatten]

(I'm starting to really like Ruby.)

In case that's not clear, scan uses a regex to provide an array of
2-element arrays from the text. Then flatten makes it into a single
array, then the * turns it into a list of just values, instead of an
array. Then Hash[] turns each 2 values into a hash.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Nice
Guest
Posts: n/a
 
      08-31-2006
William Crawford wrote:
> Brian Nice wrote:
>> I have a strings like the following:
>> s1- "[1] Hello [2] bye"
>> s2- "[1] Hello [2] bye [2:1] continue [2] more"
>>
>> I want to convert them to hashes like
>> h1- {1 => "Hello", 2 => "bye"}
>> h2- {1 => "Hello", 2 => "bye", "2:1" => "continue", "2:2" => more"}
>>
>> Is there an easy Ruby way to do this?
>> Thanks for the help
>> Brian

>
> Assuming that James is correct in that :2 is missing from the second
> string, this works:
>
> h2 = Hash[*s2.scan(/\[([^\[\]]+)\] (\w+)/).flatten]
>
> (I'm starting to really like Ruby.)
>
> In case that's not clear, scan uses a regex to provide an array of
> 2-element arrays from the text. Then flatten makes it into a single
> array, then the * turns it into a list of just values, instead of an
> array. Then Hash[] turns each 2 values into a hash.


Actually the original was correct - the second 2 should have a key of
2:2 even though it only appears in the original string as 2 (since it
follows a 2:1, it is assumed that any number after it would be 2. If
there was a 3:1 sometime later, then the next two would have a key of
3:2)

s2- "[1] Hello [2] bye [2:1] continue [2] more"

Thanks for the help
Brian

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Alex LeDonne
Guest
Posts: n/a
 
      09-06-2006
On 8/31/06, Brian Nice <(E-Mail Removed)> wrote:
> I have a strings like the following:
> s1- "[1] Hello [2] bye"
> s2- "[1] Hello [2] bye [2:1] continue [2] more"
>
> I want to convert them to hashes like
> h1- {1 => "Hello", 2 => "bye"}
> h2- {1 => "Hello", 2 => "bye", "2:1" => "continue", "2:2" => more"}
>
> Is there an easy Ruby way to do this?


Brian-

Your problem is somewhat underspecified; with more examples and info,
someone might be able to help better. Some important questions:

- may the phrases include spaces?
- can there be more than two levels?
- in your examples above, must the frist key be 1, or could it also be
"1"? Or perhaps "1:0"?
- perhaps some more background on the use case would help as well.

Good luck!

-Alex

 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments