Re: splitting string to hash

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.

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

On 8/31/06, Berger, Daniel [email protected] 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