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

64a808c19ac542b14c4021dcb9484a37?d=identicon&s=25 Ruby Student (rubystudent66)
on 2013-11-11 16:59
(Received via mailing list)
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
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-11-11 17:21
(Received via mailing list)
On Mon, Nov 11, 2013 at 4:58 PM, Ruby Student <ruby.student@gmail.com>
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.
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2013-11-11 21:46
(Received via mailing list)
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.