On Mon, Nov 11, 2013 at 4:58 PM, Ruby Student <ruby.student / gmail.com> wrote:
> 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

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.