Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string comparison 1

Status
Not open for further replies.

reggler

Programmer
Nov 13, 2008
63
0
0
CA
Hi There, I want to compare to strings to each other. My code:
Code:
while {!$cond}  {
	            #set NewStr [.serialMon.cons get MyMark end] ;#load content from console window into $
+str
	        	$GUI::twidget insert end "\n NewStr: $serial::rxBuffer!!\n" error
                $GUI::twidget see end
	        	set lst [split $serial::rxBuffer \n\r]					;#load content into list
	        	set length [llength $lst]	        	
	        	.serialMon.cons insert end "\n\n******ListLength: $length*****\n\n"
	        	if {$length<1}  {
		        	set str1 [lindex $lst [$length-1]]
		        	set str2 [lindex $lst $length]
		        	if {string compare -length 10 $str1 $str2 == 0}	;#see if the last two strings match
		        		set $cond 1										;#set stop condition
	        	}
But the interpreter is telling me following
syntax error in expression "string compare -length 10 $str1 $str2 == 0": variable references require preceding $ syntax error in expression "string compare -length 10 $str1 $str2 == 0": variable references require preceding $ ("if" test expression) while compiling "if {string compare -length 10 $str1 $str2 == 0} " ("if" then script line 4) while compiling "if {$length<1} { set str1 [lindex $lst [$length-1]] set str2 [lindex $lst $length] if {string compare -length 10 $st..." ("while" body line 8) while compiling "while {!$cond} { #set NewStr [.serialMon.cons get MyMark end] ;#load content from console window into $str $GUI::twidget inser..."
And I have no clue why, help would be appreciated! Thank you very much!

Ron
 
Hi

Code:
if {[COLOR=red pink][[/color]string compare -length 10 $str1 $str2[COLOR=red pink]][/color] == 0} [COLOR=red pink]{[/color]
  set cond 1
[COLOR=red pink]}[/color]


Feherke.
 
ah yup, now i understand: spaces and brachets are the trick... ;)
Thanks anyways!
But i can't get the spaces figured out correctly.
I have
Code:
if {[[string length $str1] > 10] && [[string length $str2] > 10]}
And the interpreter tells me:

invalid command name "0"
invalid command name "0"
while executing
"[string length $str1] > 10"

What is it not understanding? .... 0 is not larger than 10 so make it false.... but... :(
 
[]" is only for command substitution
if {[[string length $str1] > 10] && [[string length $str2] > 10]}
should be
Code:
if {[red]([/red][string length $str1] > 10[red])[/red] && [red]([/red][string length $str2] > 10[red])[/red]}

_________________
Bob Rashkin
 
alright, this underlines it's all about the brackets :)

Thanks buddy for this hint! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top