Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Pattern seraching/matching

Posted by smallsteps (smallsteps), 21 September 2007
Hi There,

I am facing some problems in TCL scripting:

I need to capture a pattern in the a string like

"<?xml version="1.0" encoding="UTF-8" ?><CommandList><Response><Interface>cs2kCfgMgrIf</Interface><Methods><assocMG usn="1" version=" 1.0"><ReturnData><RC>38</RC><MsgTxt>The MG is already provisionedThe Gateway name is already defined on GWC-7</MsgTxt></ReturnData></assocMG></Methods></Response></CommandList>"

Ideally I would like to capture the Return data as it an error code from the command thats sent and if the return code is not 0 then it should break. So in this case; <RC>38</RC> and so on. But my script doesn't do that. My script is as below:

if {[string match "<RC>0</RC>" "$response"] == 0 } {
        #send a message back that it was okay
       } else {
              #send a message back that it failed
               break
               # exit the script.
               exit
       }

Could you please tell me what am I doing wrong here.

Thanks in advance,




Posted by admin (Graham Ellis), 21 September 2007
Firstly, "break" breaks you out of a loop ... but you have no loop in the sample code supplied.  I believe that it's redundant and the exit is all you need to leave the script.

If you want to capture the error string, then I would advise you to do so using a regexp command along the lines of
regexp <ReturnData>.*</ReturnData> $response target
which should capture the return data part of your string into a variable called target.  Your current code doesn't identify the "interesting bit" of the incoming string in any way ... it makes no attempt to extract it ... and that's why your program doesn't make any specific use of it.

Posted by smallsteps (smallsteps), 21 September 2007
Thanks Graham. Got the mistake. Would try it out.

Posted by smallsteps (smallsteps), 21 September 2007
Hi Graham,

It worked thanks. Another basic question is that I need to read values only in between the tags.

eg:
<RC>0</RC> , Here only value 0 is interesting for me.
or <RC>1401</RC>, in this case only 1401 is interesting to me.

There are several tags like this where I need to check for the values inside the tags. Now I can read the complete tags.

Thanks in advance.

Posted by admin (Graham Ellis), 21 September 2007
You'll need to use the submatch vars - have a look at the regexp manual at

http://www.tcl.tk/man/tcl8.4/TclCmd/regexp.htm


Basically, you put round brackets around the interesting bits that you want to capture, and then suppl an extra variable name on the end on which that intersting bit is to be stored

Posted by smallsteps (smallsteps), 24 September 2007
Hi Graham,

Somehow am stuck.


set response { <RC>0</RC> }
puts "response $response"
regexp {>(.*)</}  $response target
puts "target is $target"


Result:

response  <RC>0</RC>
target is >0</


I want to get rid of the characters around 0 . Could you please advice?

Posted by admin (Graham Ellis), 24 September 2007
Replace

regexp {>(.*)</}  $response target

with

regexp {>(.*)</}  $response everything target

The first extra variable contains the entire match, and subsequent ones the parts of the incoming string that matched each of the regular expression "interesting bits" it turn.

Posted by smallsteps (smallsteps), 26 September 2007
thanks



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho