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

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

On Wed, Dec 3, 2008 at 10:12 AM, Vrone Ve [email protected] wrote:

Hi all

string = “abc/ def/ ghi/”

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

See String#split and String#strip.

Regards,
Craig

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

cheers
irb> string = “abc/ def/ ghi/”
=> “abc/ def/ ghi/”
irb> 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 B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

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

cheers
irb> string = “abc/ def/ ghi/”
=> “abc/ def/ ghi/”
irb> 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 B. http://agileconsultingllc.com
[email protected]

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

Vrone Ve wrote:

Rob B. wrote:

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

cheers
irb> string = “abc/ def/ ghi/”
=> “abc/ def/ ghi/”
irb> 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 B. http://agileconsultingllc.com
[email protected]

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:52 AM, Vrone Ve wrote:

really
thanks Rob,
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:

irb> “ABC\Name X\Gender”.split(’ ').map{|pair| pair.split(/
/).reverse }
=> [[“Name”, “ABC”], [“Gender”, “X”]]

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

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

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

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

irb> "ABC\Name X\Gender Z\Person ".scan(/([^\]+)\(\w+)\s?/)
=> [[“ABC”, “Name”], [“X”, “Gender”], [“Z”, “Person”]]
irb> alist = _
=> [[“ABC”, “Name”], [“X”, “Gender”], [“Z”, “Person”]]
irb> alist.rassoc(‘Name’)
=> [“ABC”, “Name”]
irb> alist.rassoc(‘Person’)
=> [“Z”, “Person”]
irb> 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 B. http://agileconsultingllc.com
[email protected]

Vrone Ve wrote:

Vrone Ve wrote:

Rob B. wrote:

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

cheers
irb> string = “abc/ def/ ghi/”
=> “abc/ def/ ghi/”
irb> 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 B. http://agileconsultingllc.com
[email protected]

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”]]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs