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.
Ff4b5bfd0b72d9d153d33d8d26ed126c?d=identicon&s=25 Vrone Ve (vrone)
on 2008-12-03 16: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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-12-03 16: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
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-12-03 16:39
(Received via mailing list)
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??


See String#split and String#strip.

Regards,
Craig
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-12-03 16: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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Ff4b5bfd0b72d9d153d33d8d26ed126c?d=identicon&s=25 Vrone Ve (vrone)
on 2008-12-03 16:58
Rob Biedenharn 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 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-12-03 18: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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Ff4b5bfd0b72d9d153d33d8d26ed126c?d=identicon&s=25 Vrone Ve (vrone)
on 2008-12-03 18:09
Vrone Ve wrote:
> Rob Biedenharn 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 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
9440222a0c3e33f369eef79712e12204?d=identicon&s=25 David Wright (__dw5__)
on 2008-12-03 18:34
Vrone Ve wrote:
> Vrone Ve wrote:
>> Rob Biedenharn 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 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"]]
Ff4b5bfd0b72d9d153d33d8d26ed126c?d=identicon&s=25 Vrone Ve (vrone)
on 2008-12-03 18: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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-12-03 21: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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
This topic is locked and can not be replied to.