Is there an easier way to parse into tokens a string?

Hello Team,

When running the piece of code below, I expect and get the following
line
of text, which comes as a one element array.
[“AMQ8409: Display Queue details.\n QUEUE(TEMP) TYPE(QLOCAL)\n
CURDEPTH(6)”]

I am only interested in the queue depth, which is pointed to by
CURDEPTH.
That number, which is 6 for case, is the only thing I want.
I convert that one element into a string and then parse (tokenize) it
using
the split method. As you can see it takes me 3 operations to get that
number.
Using my method below on a very long string will make it unmanageable.
Think of a string with few dozen words!
Is there a more simple Ruby way to parse the string and get what I want?

Bellow is the actual code.

Thank you

require ‘rubygems’
require ‘wmq’

WMQ::QueueManager.connect(:q_mgr_name=>‘WMQDEVA1’,
:connection_name => ‘test.host(1414)’,
:channel_name => ‘WMQDEVA1.ADM.SVRCONN’) do
|qmgr|

The next stmt returns an array of 1 element, which I convert to a

string

I am ONLY interested in the content of CURDEPTH, the number between

parenthesis

Actual output:

*["AMQ8409: Display Queue details.\n QUEUE(TEMP)

TYPE(QLOCAL)\n
CURDEPTH(6)"]*
cdepth = qmgr.mqsc(‘dis ql(TEMP) CURDEPTH’).to_s
v1,v2,v3,v4,v5,v6,v7 = cdepth.split(" “) # tokenize string to
isolate
CURDEPTH(6)
v1,v2 = v7.split(”(") # Remove left parenthesis
qdepth, gb = v2.split(")") # Remove right parenthesis
puts “CURDEPTH = #{qdepth}” # 6
end

Thank you

On Mon, Nov 11, 2013 at 4:58 PM, Ruby S. [email protected]
wrote:

the split method. As you can see it takes me 3 operations to get that
require ‘wmq’
CURDEPTH(6)"]
cdepth = qmgr.mqsc(‘dis ql(TEMP) CURDEPTH’).to_s
v1,v2,v3,v4,v5,v6,v7 = cdepth.split(" “) # tokenize string to isolate
CURDEPTH(6)
v1,v2 = v7.split(”(") # Remove left parenthesis
qdepth, gb = v2.split(")") # Remove right parenthesis
puts “CURDEPTH = #{qdepth}” # 6
end

You could do it with a regular expression and a capturing group:

2.0.0-p195 :003 > s = “AMQ8409: Display Queue details.\n QUEUE(TEMP)
TYPE(QLOCAL)\n CURDEPTH(6)”
=> “AMQ8409: Display Queue details.\n QUEUE(TEMP)
TYPE(QLOCAL)\n CURDEPTH(6)”
2.0.0-p195 :005 > s[/CURDEPTH(([^)]*))/,1]
=> “6”

Jesus.

On Mon, 2013-11-11 at 17:20 +0100, Jesús Gabriel y Galán wrote:

I convert that one element into a string and then parse (tokenize) it using
require ‘rubygems’

["AMQ8409: Display Queue details.\n QUEUE(TEMP) TYPE(QLOCAL)\n

2.0.0-p195 :003 > s = “AMQ8409: Display Queue details.\n QUEUE(TEMP)
TYPE(QLOCAL)\n CURDEPTH(6)”
=> “AMQ8409: Display Queue details.\n QUEUE(TEMP)
TYPE(QLOCAL)\n CURDEPTH(6)”
2.0.0-p195 :005 > s[/CURDEPTH(([^)]*))/,1]
=> “6”

Jesus.

irb(main):015:0> s = “AMQ8409: Display Queue details.\n QUEUE(TEMP)
TYPE(QLOCAL)\n CURDEPTH(6)”
=> “AMQ8409: Display Queue details.\n QUEUE(TEMP)
TYPE(QLOCAL)\n CURDEPTH(6)”

irb(main):016:0> s.slice(s.rindex(/(/)+1…s.rindex(/)/)-1)
=> “6”
irb(main):017:0>


If you have received the message in error, please advise the sender by
reply email and please delete the message. This message contains
information which may be confidential or otherwise protected. Unless
you are the addressee (or authorized to receive for the addressee), you
may not use, copy, or disclose to anyone the message or any information
contained in the message.

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