Forum: Ruby string with \

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Vrone V. (Guest)
on 2008-12-03 17:18
Hi all

string = "abc/ def/ ghi/"

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

cheers
Brian C. (Guest)
on 2008-12-03 17:36
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
Craig D. (Guest)
on 2008-12-03 17:39
(Received via mailing list)
On Wed, Dec 3, 2008 at 10:12 AM, Vrone Ve <removed_email_address@domain.invalid> 
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
Rob B. (Guest)
on 2008-12-03 17:46
(Received via mailing list)
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
removed_email_address@domain.invalid
Vrone V. (Guest)
on 2008-12-03 17:58
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
> removed_email_address@domain.invalid


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
Rob B. (Guest)
on 2008-12-03 19:07
(Received via mailing list)
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
removed_email_address@domain.invalid
Vrone V. (Guest)
on 2008-12-03 19:09
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
>> removed_email_address@domain.invalid

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
David W. (Guest)
on 2008-12-03 19:34
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
>>> removed_email_address@domain.invalid
>
> 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"]]
Vrone V. (Guest)
on 2008-12-03 19:54
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
Rob B. (Guest)
on 2008-12-03 22:27
(Received via mailing list)
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. ;-)

-Rob


Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.