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 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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., 2022: 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