string with \

V

Vrone Ve

Hi all

string = "abc/ def/ ghi/"

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

cheers
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

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
 
R

Rob Biedenharn

Hi all

string = "abc/ def/ ghi/"

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

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
(e-mail address removed)
 
V

Vrone Ve

Rob said:
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
(e-mail address removed)


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
 
R

Rob Biedenharn

Rob said:
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
(e-mail address removed)

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:

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

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
V

Vrone Ve

Vrone said:
Rob said:
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
(e-mail address removed)

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
 
D

David Wright

Vrone said:
Vrone said:
Rob said:
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
(e-mail address removed)

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"]]
 
V

Vrone Ve

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
 
R

Rob Biedenharn

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,578
Latest member
LC_06

Latest Threads

Top