# Re: splitting string to hash

**URL:** <https://www.ruby-forum.com/t/re-splitting-string-to-hash/67100>\
**Category:** Ruby\
**Created:** [August 31, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/re-splitting-string-to-hash/67100 "2006-08-31T16:56:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [August 31, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/re-splitting-string-to-hash/67100/1 "2006-08-31T16:56:13Z")

</div>

> s2- “[1] Hello [2] bye [2:1] continue [2] more”  
> Should that be “[2:2] more” ?

> I want to convert them to hashes like  
> h1- {1 =\> “Hello”, 2 =\> “bye”}

arr = s1.split(/\s\*?[(._?)]\s_/)  
arr.delete("")

h1 = Hash[\*arr]

I’m not actually sure why I get an empty string from the Array#split  
call. If someone can figure that out, you can get rid of the  
Array#delete call.

Regards,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [August 31, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/re-splitting-string-to-hash/67100/2 "2006-08-31T17:14:38Z")

</div>

No, that was actually correct, the second one is [2] not [2:2]. The key  
would need to be 2:2 though otherwise it could possibly override another  
key.  
Thanks for the other one!

Berger, Daniel wrote:

> > s2- “[1] Hello [2] bye [2:1] continue [2] more”  
> > Should that be “[2:2] more” ?
> 
> > I want to convert them to hashes like  
> > h1- {1 =\> “Hello”, 2 =\> “bye”}
> 
> arr = s1.split(/\s\*?[(._?)]\s_/)  
> arr.delete("")
> 
> h1 = Hash[\*arr]
> 
> I’m not actually sure why I get an empty string from the Array#split  
> call. If someone can figure that out, you can get rid of the  
> Array#delete call.
> 
> Regards,
> 
> Dan

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [September 6, 2006, 4:58pm UTC](https://www.ruby-forum.com/t/re-splitting-string-to-hash/67100/3 "2006-09-06T16:58:15Z")

</div>

On 8/31/06, Berger, Daniel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > s2- “[1] Hello [2] bye [2:1] continue [2] more”
> 
> I’m not actually sure why I get an empty string from the Array#split  
> call. If someone can figure that out, you can get rid of the  
> Array#delete call.

If you’re capturing a group in your split pattern, and your pattern  
matches the beginning of the string, you’ll get the leading null field  
in your array. By default, a trailing null field is suppressed; you  
can get at it by setting the optional limit param to -1.

irb(main):001:0\> “1234”.split(/(\d)/)  
=\> [“”, “1”, “”, “2”, “”, “3”, “”, “4”]  
irb(main):002:0\> “1234”.split(/(\d)/, -1)  
=\> [“”, “1”, “”, “2”, “”, “3”, “”, “4”, “”]

-Alex
