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))
tcl proc to string match data to the stdout data

Posted by deepshikha_dutta (deepshikha_dutta), 26 September 2007
Hi,

I have a question ,  I want to create a script which will execute in the following way.

tcl> regress {ls} {check "file1"}

regress is a proc.
"ls" is a shell command to list out all files in the directory.
"check is a proc " which will string match "file1" to the data generated by "ls"

Wht I want to do:
------------------------
When I will run the above format : regress will check the file "file1"  from the list of files at the stdout due to "execution of ls" and will pass if the file exist or will fail if the file does not exist.

Could someone help out.

Regards
Deep

Posted by admin (Graham Ellis), 26 September 2007
So you're looking for a proc that will check whether or not the first parameter is one of the values in a list given as the second parameter?  

Posted by deepshikha_dutta (deepshikha_dutta), 26 September 2007
For ex:
---------
"ls" on tcl prompt gives the following set of files :

tcl>ls
file12 file13 file23 ddd actual

Now I want to search for "file1" from the above list.
if match no error if not generates error.

Here file1 match with both "file12 & file13" so no error.

I want to exec  in this manner:
regress {ls} {check "file1"}

note:regress & check both are proc.
"ls" is the argument pass to "proc regress"
and "proc check" match "file1" with the output of "ls"  

Regards
Deep  

Posted by admin (Graham Ellis), 26 September 2007
Ah - so you're looking to see whether the first value if WITHIN the second.

I think all you need is a string match:

Code:
proc regress {needle haystack} {
       set result [string match "*$needle*" $haystack]
       return $result
       }

set within "this that file2 file19 16 bill"

set find file1
set status [regress $find $within]
puts "Status for $find is $status"

set find orange
set status [regress $find $within]
puts "Status for $find is $status"

set find that
set status [regress $find $within]
puts "Status for $find is $status"


Which runs as

Code:
earth-wind-and-fire:~/sep07 grahamellis$ tclsh regr
Status for file1 is 1
Status for orange is 0
Status for that is 1
earth-wind-and-fire:~/sep07



Posted by deepshikha_dutta (deepshikha_dutta), 26 September 2007
Thanks,

Now you have hard coded the list  
set within "this that file2 file19 16 bill"

I have modified a little bit as follows and it works:

set within [exec ls]  

Now You have given output as 1 & 0 .

Actually I want the command regress to fail by giving out error message  when string match does not happen.

Regards
Deep


Posted by admin (Graham Ellis), 27 September 2007
on 09/26/07 at 20:10:45, deepshikha_dutta wrote:

Now You have given output as 1 & 0 .

Actually I want the command regress to fail by giving out error message  when string match does not happen.



You are very welcome to change my example code to do whatever you need  

Posted by deepshikha_dutta (deepshikha_dutta), 27 September 2007
Thanks for the code it is going to be helpfull.

Regards
debraj  



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