# Splitting string to hash

**URL:** <https://rubytalk.org/t/splitting-string-to-hash/30518>\
**Category:** ruby-talk\
**Created:** [31 August 2006 14:38 UTC](https://rubytalk.org/t/splitting-string-to-hash/30518 "2006-08-31T14:38:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Nice](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Brian\_Nice](https://rubytalk.org/u/Brian_Nice)\
**Post date:** [31 August 2006 14:38 UTC](https://rubytalk.org/t/splitting-string-to-hash/30518/1 "2006-08-31T14:38:30Z")

</div>

I have a strings like the following:  
&nbsp;&nbsp;s1- "[1] Hello [2] bye"  
&nbsp;&nbsp;s2- "[1] Hello [2] bye [2:1] continue [2] more"

I want to convert them to hashes like  
&nbsp;&nbsp;h1- {1 =\> "Hello", 2 =\> "bye"}  
&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [31 August 2006 14:43 UTC](https://rubytalk.org/t/splitting-string-to-hash/30518/2 "2006-08-31T14:43:58Z")

</div>

I assume more is missing a quote there.

Also, how was the 2:2 key determined?

James Edward Gray II

> **···**
>
> On Aug 31, 2006, at 9:38 AM, Brian Nice wrote:
> 
> > &nbsp;&nbsp;s2- "[1] Hello [2] bye [2:1] continue [2] more"
> 
> > &nbsp;&nbsp;h2- {1 =\> "Hello", 2 =\> "bye", "2:1" =\> "continue", "2:2" =\> more"}

---

<div class="post-metadata">

**Author:** ![William\_Crawford](https://avatars.discourse-cdn.com/v4/letter/w/dbc845/32.png) [@William\_Crawford](https://rubytalk.org/u/William_Crawford)\
**Post date:** [31 August 2006 15:20 UTC](https://rubytalk.org/t/splitting-string-to-hash/30518/3 "2006-08-31T15:20:39Z")

</div>

Brian Nice wrote:

> I have a strings like the following:  
> &nbsp;&nbsp;s1- "[1] Hello [2] bye"  
> &nbsp;&nbsp;s2- "[1] Hello [2] bye [2:1] continue [2] more"
> 
> I want to convert them to hashes like  
> &nbsp;&nbsp;h1- {1 =\> "Hello", 2 =\> "bye"}  
> &nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![A\_LeDonne](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@A\_LeDonne](https://rubytalk.org/u/A_LeDonne)\
**Post date:** [6 September 2006 14:36 UTC](https://rubytalk.org/t/splitting-string-to-hash/30518/4 "2006-09-06T14:36:27Z")

</div>

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

> **···**
>
> On 8/31/06, Brian Nice \<bnice\_99@yahoo.com\> wrote:
> 
> > I have a strings like the following:  
> > &nbsp;&nbsp;s1- "[1] Hello [2] bye"  
> > &nbsp;&nbsp;s2- "[1] Hello [2] bye [2:1] continue [2] more"
> > 
> > I want to convert them to hashes like  
> > &nbsp;&nbsp;h1- {1 =\> "Hello", 2 =\> "bye"}  
> > &nbsp;&nbsp;h2- {1 =\> "Hello", 2 =\> "bye", "2:1" =\> "continue", "2:2" =\> more"}
> > 
> > Is there an easy Ruby way to do this?

---

<div class="post-metadata">

**Author:** ![Brian\_Nice](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Brian\_Nice](https://rubytalk.org/u/Brian_Nice)\
**Post date:** [31 August 2006 18:49 UTC](https://rubytalk.org/t/splitting-string-to-hash/30518/5 "2006-08-31T18:49:23Z")

</div>

William Crawford wrote:

> Brian Nice wrote:
> 
> > I have a strings like the following:  
> > &nbsp;&nbsp;s1- "[1] Hello [2] bye"  
> > &nbsp;&nbsp;s2- "[1] Hello [2] bye [2:1] continue [2] more"
> > 
> > I want to convert them to hashes like  
> > &nbsp;&nbsp;h1- {1 =\> "Hello", 2 =\> "bye"}  
> > &nbsp;&nbsp;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:x. 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/\](http://www.ruby-forum.com/%5C).
