String with \

Hi all

string = "abc/ def/ ghi/"

how to get all three of these dynamic values placed before backslash in
separate variables.
any suggestitons??

cheers

···

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

Vrone Ve wrote:

Hi all

string = "abc/ def/ ghi/"

how to get all three of these dynamic values placed before backslash in
separate variables.
any suggestitons??

http://www.ruby-doc.org/docs/ProgrammingRuby/

Look at String#split, i.e. the split method of the String class

···

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

See String#split and String#strip.

Regards,
Craig

···

On Wed, Dec 3, 2008 at 10:12 AM, Vrone Ve <vrone@hotmail.co.uk> wrote:

Hi all

string = "abc/ def/ ghi/"

how to get all three of these dynamic values placed before backslash in
separate variables.
any suggestitons??

Hi all

string = "abc/ def/ ghi/"

how to get all three of these dynamic values placed before backslash in
separate variables.
any suggestitons??

cheers

string = "abc/ def/ ghi/"

=> "abc/ def/ ghi/"

string.split(%r{/\s*})

=> ["abc", "def", "ghi"]

You could use parallel assignment, but I'd question whether you really wanted three variables rather than a three-element array.

a, d, g = string.split(%r{/\s*})

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:

Rob Biedenharn wrote:

cheers

> string = "abc/ def/ ghi/"
=> "abc/ def/ ghi/"
> string.split(%r{/\s*})
=> ["abc", "def", "ghi"]

You could use parallel assignment, but I'd question whether you really
wanted three variables rather than a three-element array.

a, d, g = string.split(%r{/\s*})

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

thanks Rob,

My detailed question is regarding a string something like
String "ABC\Name X\Gender "
and I want to scan the string for each of the element(i.e. Name, Gender)
and whereever Name is written in the string, I want to grab the value
placed before the back slash and store it in a variable. same is the
case with gender and/or with any other attribute i want to be in this
string.

Cheers

···

On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:

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

Rob Biedenharn wrote:

cheers

> string = "abc/ def/ ghi/"
=> "abc/ def/ ghi/"
> string.split(%r{/\s*})
=> ["abc", "def", "ghi"]

You could use parallel assignment, but I'd question whether you really
wanted three variables rather than a three-element array.

a, d, g = string.split(%r{/\s*})

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

thanks Rob,

My detailed question is regarding a string something like
String "ABC\Name X\Gender "
and I want to scan the string for each of the element(i.e. Name, Gender)
and whereever Name is written in the string, I want to grab the value
placed before the back slash and store it in a variable. same is the
case with gender and/or with any other attribute i want to be in this
string.

Cheers

Then I presume you want a transformation something like:

magic("ABC\\Name X\\Gender")
=> [["Name", "ABC"], ["Gender", "X"]]
or
=> { "Name" => "ABC", "Gender" => "X" }

You can then manipulate the alist or hash (key-value pairs) as you wish. (for the alist, "association list", you can use the Array#assoc method)

Depending on where whitespace is permitted in the original string, you might be able to:

"ABC\\Name X\\Gender".split(' ').map{|pair| pair.split(/\

\/).reverse }
=> [["Name", "ABC"], ["Gender", "X"]]

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Dec 3, 2008, at 10:52 AM, Vrone Ve wrote:

On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:

Vrone Ve wrote:

Rob Biedenharn wrote:

cheers

> string = "abc/ def/ ghi/"
=> "abc/ def/ ghi/"
> string.split(%r{/\s*})
=> ["abc", "def", "ghi"]

You could use parallel assignment, but I'd question whether you really
wanted three variables rather than a three-element array.

a, d, g = string.split(%r{/\s*})

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

thanks Rob, and all for your response

My detailed question is regarding a string something like
String "ABC\Name X\Gender "
and I want to scan the string for each of the element(i.e. Name, Gender)
and whereever Name is written in the string, I want to grab the value
placed before the back slash and store it in a variable. same is the
case with gender and/or with any other attribute i want to be in this
string.

Your help required.
cheers

···

On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:

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

Vrone Ve wrote:

Vrone Ve wrote:

Rob Biedenharn wrote:

cheers

> string = "abc/ def/ ghi/"
=> "abc/ def/ ghi/"
> string.split(%r{/\s*})
=> ["abc", "def", "ghi"]

You could use parallel assignment, but I'd question whether you really
wanted three variables rather than a three-element array.

a, d, g = string.split(%r{/\s*})

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

thanks Rob, and all for your response

My detailed question is regarding a string something like
String "ABC\Name X\Gender "
and I want to scan the string for each of the element(i.e. Name, Gender)
and whereever Name is written in the string, I want to grab the value
placed before the back slash and store it in a variable. same is the
case with gender and/or with any other attribute i want to be in this
string.

Your help required.
cheers

rather then split, I believe you want scan.

irb(main):032:0> str = 'ABC\Name X\Gender Z\Person '
=> "ABC\\Name X\\Gender Z\\Person "
irb(main):033:0> str.scan(/(.*?)\\\w+\s?/)
=> [["ABC"], ["X"], ["Z"]]

irb(main):034:0> t = str.scan(/(.*?)\\\w+\s?/)
=> [["ABC"], ["X"], ["Z"]]
irb(main):035:0> t
=> [["ABC"], ["X"], ["Z"]]

···

On Dec 3, 2008, at 10:12 AM, Vrone Ve wrote:

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

What if There is some optional value which may appear in strings
oftenly, and i want to check against its attribute value. or in other
words its not necesary to have Name with its value every time in the
string, and i want to have some checks on it ( e.g if Name attribute is
there then grab its value before the back slash, then moving along all
other attributes and chk there occurance take their value)

thanks again

···

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

What if There is some optional value which may appear in strings
oftenly, and i want to check against its attribute value. or in other
words its not necesary to have Name with its value every time in the
string, and i want to have some checks on it ( e.g if Name attribute is
there then grab its value before the back slash, then moving along all
other attributes and chk there occurance take their value)

thanks again

"ABC\\Name X\\Gender Z\\Person ".scan(/([^\\]+)\\(\w+)\s?/)

=> [["ABC", "Name"], ["X", "Gender"], ["Z", "Person"]]

alist = _

=> [["ABC", "Name"], ["X", "Gender"], ["Z", "Person"]]

alist.rassoc('Name')

=> ["ABC", "Name"]

alist.rassoc('Person')

=> ["Z", "Person"]

alist.rassoc('Optional')

=> nil

If you still can't figure it out, either hire someone or at least post your actual problem with some real data. Even a little code fragment that you think is close, but perhaps misbehaving in a few cases. Showing those cases as tests would be even better. :wink:

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Dec 3, 2008, at 12:49 PM, Vrone Ve wrote: