Message: 8
Subject: InStr problems
From: Aubrey Todd <atodd at copper dot net>
Date: Thu, 27 May 2004 11:41:04 -0700
I am having a problem with the InStr command in the following code:
Dim Pos as Integer
//Check for Justaposition on the left
//Checks for a number before the '(' and inserts a '*' if appropriate
Pos=-1
Do //This loop works fine
Pos=Pos+2
Pos=InStr(Pos,ProbStr,"(")
If Pos>1 Then
perform some process here
End If
Loop Until Pos=0
//Check for Justaposition on the right
//Checks for a number after the ')' and inserts a '*' if appropriate
Pos=-1
Do //This loop does not work all the time
Pos=Pos+2
Pos=InStr(Pos,ProbStr,")") //<===Problem occurs here
If Pos>1 Then
perform some process here
End If
Loop Until Pos=0
ProbStr is a string property.
If ProbStr=(2+1/2)+(3+1/4) both loops work correctly.
If ProbStr=Dec(2+1/2)+Dec(3+1/4), the second loop does not exit because
Pos never goes to zero.
In the second loop, Pos=InStr(Pos,ProbStr,")")=21 the second time
through, then loop and Pos=Pos+2=23, but when it hits
Pos=InStr(Pos,ProbStr,")") again, Pos is set to 21 instead of zero. If
I manually change the command to Pos=InStr(23,ProbStr,")"), it works
fine and returns a zero.
Anyone have a hint as to what I am doing wrong or why it does not work?
Thanks,
Aubrey
PB G4 Mac OS 10.3.3
RB 5.2.4
==================================================
Message: 9
Subject: Re: InStr problems
From: "Joseph J. Strout" <joe at realsoftware dot com>
Date: Thu, 27 May 2004 13:47:48 -0500
At 11:41 AM -0700 5/27/04, Aubrey Todd wrote:
Do //This loop does not work all the time
Pos=Pos+2
Pos=InStr(Pos,ProbStr,")") //<===Problem occurs here
If Pos>1 Then
perform some process here
End If
Loop Until Pos=0
...
In the second loop, Pos=InStr(Pos,ProbStr,")")=21 the second time
through, then loop and Pos=Pos+2=23, but when it hits
Pos=InStr(Pos,ProbStr,")") again, Pos is set to 21 instead of zero.
You've verified this by stepping through it with the debugger?
If I manually change the command to Pos=InStr(23,ProbStr,")"), it
works fine and returns a zero.
In that case, there has to be a some sort of confusion here; there is
no difference to InStr between a literal 23 and a variable which
contains 23.
My guess is that the second time through the loop, Pos is not
actually equal to 21. Perhaps "perform some process here" is
accidentally clobbering the value of Pos.
Best,
- Joe