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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string compare 1

Status
Not open for further replies.

reggler

Programmer
Nov 13, 2008
63
0
0
CA
Hi there,
I have something weird happening to me and i have no clue where to go.... :eek:
My situation: I have 3 strings. If either $str1 and $str2 or $str2 and $str3 match, I set a flag and continue but i have totally different string contents and my if clause still returns true... huh???
My code looks like this:
Code:
if { ([string compare -length 7 $str1 $str2] == 0) || ([string compare -length 2 $str2 $str3]) } {				        	
			        		set cond 0
			        		$GUI::twidget insert end "\n\n***MATCH***str1:$str1 = str2:$str2 ||str2:$str2 = str3:$str3\n\n"

    	    		}
and this is what I get in my text widget ($GUI::twidget): ***MATCH***str1:temperature = str2:>memrw 3 ||str2:>memrw 3 = str3:

isn't that weird? :eek: Can any one explain what's happening here?
I also checked the strings' length to make sure there's no unprintable characters that match but the sizes look like this:

len1: 11
len2: 8
len3: 0

figured out with
Code:
		        	set len1 [string length $str1]
		        	set len2 [string length $str2]
		        	set len3 [string length $str3]

I'm very thankful for any hints or suggestions!
Thanks,
Ron
 
Hi reggler,
It don't believe. I tried this with your 3 strings
Code:
set string1 "temperature"
set string2 ">memrw 3"
set string3 ""

if {([string compare -length 7 $string1 $string2] == 0) || 
    ([string compare -length 2 $string2 $string3] == 0)} {
  puts "They are equal"
} else {
  puts "They are not equal"
}

and I get
They are not equal
 
Hm yeah,

That's what you would expect but I unfortunately get repeatedly the wrong result :eek:
is
set myvar [string length string] also counting unprintable characters?
I'm on EST which means my week is over, I will follow-up on Monday but every body that may have an idea is invited to contribute by speaking out :)
Thanks,
Ron
 
>set myvar [string length string] also counting unprintable characters?
Here is the experiment: I have 2 chars:
\0 - null string terminator from C: 0x00
\7 - beep: 0x07
Code:
set mystring "\0\7"
puts "mystring = '$mystring'"
puts "len(mystring)=[string length $mystring]"
result:
Code:
mystring = ' '
len(mystring)=2
 
So can anyone explain what I'm getting then?
My console output looks like this:
Code:
...
...
len1: 11
len2: 8
len3: 0

***size check is true!!!



***MATCH***str1:temperature = str2:>memrw 3 ||str2:>memrw 3 = str3:



*****************BIST COMPLETE******************
and this is my code
Code:
set rslt -1
    set serial::rxBuffer {}
    while {$rslt == -1}  {
        #load content from console window into $str
	    #set str [.serialMon.cons get 0.0 end]
        #search for the string "Pass: 10" in $str
        set rslt [string first "Pass: $BIS_Test::total_iterations" $serial::rxBuffer]    
        if {$rslt != -1} {                         
        	set cond 1
            while {$cond}  {		                        
                $GUI::twidget see end
                #load content from console window into a list (break at new line)
	        	set lst [split $serial::rxBuffer \n\r]
	        	set length [llength $lst]	        		        	
	        	#debug - $GUI::twidget insert end "\n\n***list length: $length***\n\n"
	        	if {$length>1}  {		        	
		        	set str1 [string range [lindex $lst [expr $length - 4]] 0 10]
		        	set str2 [string range [lindex $lst [expr $length - 2]] 0 10]
		        	set str3 [string range [lindex $lst [expr $length - 1]] 0 10]
		        	set len1 [string length $str1]
		        	set len2 [string length $str2]
		        	set len3 [string length $str3]
		        	$GUI::twidget insert end "\n\nstr1: $str1\nstr2: $str2\nstr3: $str3\n\nlen1: $len1\nlen2: $len2\nlen3: $len3"
		        	if {(([string length $str1] > 7) && ([string length $str2] > 7)) || (([string length $str3] > 0) && ([string length $str2] >0))}  {
			        	$GUI::twidget insert end "\n\n***size check is true!!!\n\n"
		        		#see if the last two strings from the console window match set stop condition
		        		if { ([string compare -length 7 $str1 $str2] == 0) || ([string compare -length 2 $str2 $str3]) } {				        	
			        		set cond 0
			        		$GUI::twidget insert end "\n\n***MATCH***str1:$str1 = str2:$str2 ||str2:$str2 = str3:$str3\n\n"
	    	    		}
    	    		}
	        	}
            	finaltest::delay 1000
            }
        }
        finaltest::delay 1000
    }
    .serialMon.cons insert end "\n\n*****************BIST COMPLETE******************\n"
Sorry for the long post but i'm stuck... does anyone have an idea how i can achieve this with another solution?
Thank you,

What I'm doing:
- waitting until I see "Pass: $BIS_Test::total_iteration" in the RxBuffer
- now i wait until i see two similar lines on the bottom - or the 2nd last and the 4th last the same (because the last lines in a specific case contain newline chars)...

Thank you for suggestions!

Ron
 
Reggler !!!
[machinegun]

You forgot [red]==[/red] in your second [string compare..]!
Look here:
Code:
set str1 "temperature"
set str2 ">memrw 3"
set str3 ""

puts "BAD CODE:"
if { ([string compare -length 7 $str1 $str2] == 0) || 
     ([string compare -length 2 $str2 $str3]) } {                             
  set cond 0
  puts "\n\n***MATCH***str1:$str1 = str2:$str2 ||str2:$str2 = str3:$str3\n\n"
}

puts "GOOD CODE:"
if { ([string compare -length 7 $str1 $str2] == 0) || 
     ([string compare -length 2 $str2 $str3] [red]==[/red] 0) } {                             
  set cond 0
  puts "\n\n***MATCH***str1:$str1 = str2:$str2 ||str2:$str2 = str3:$str3\n\n"
}
Correct it and don't forget give me the STAR
[2thumbsup]
 
I think you may be confused about what string compare returns. Either that or you left out a ==0 in the second sub-clause. From the help file:
Returns -1, 0, or 1, depending on whether string1 is lexicographically less than, equal to, or greater than string2.

Your conditional clause in the if block is true when str2 is greater than str3 which is blank so anything is greater.

_________________
Bob Rashkin
 
reggler wants to compare if the strings are equal, but because
Code:
puts [expr [string compare -length 7 $str1 $str2] == 0] 
puts [string compare -length 2 $str2 $str3]
gives
Code:
0
1
he got from the OR delivered
Code:
0 || 1 = 1
which means true


 
You were too quick for me. I was talking to the OP.

_________________
Bob Rashkin
 
Yup, yup, yup, thank you for the feedback. This actually helped a lot! I was able to fix this!
Thank you x 1000! Man I didn't see this.... GRR....drove me crazy....oh well....i'm sorry to have wasted everybody's time and say again

THANK YOU!
 
Original Post(er).

_________________
Bob Rashkin
 
Hi Bong,
Thanx for the explanation of the abreviation.
I try to improve a my english.
 
That doesn't have anything to do with one's English skills but rather with your Usenet-ish skills - I believe ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top