Please take a look at this code

jschuchert

Active Member
Licensed User
Longtime User
Below are 5 lines of comma-delimited text which I wish to manipulate with the code below it. The lines of text constitute the file 'strfilename'

1,1000,1000,none
2,1027.699,1030.228,none
3,5000,5000,iron rod
4,993.547,6987.254,none

B4X:
Sub Globals
   'Declare the global variables here.
   Public dblnorth1,dbleast1,dblndiff,dblediff,dblbearing,dblbearingdms
   Public dblnorth2,dbleast2
   Dim firstpt,secondpt
   Dim coord(4)  '[color=red]array for variable 'coord'[/color]
    radian=cPI/180
    radianreverse=180/cPI
End Sub


Sub startinverse8(strbearing)
FileClose(c)
FileOpen(c, main.strfilename,cRead)
pintcntr = StrIndexOf(strBearing, "*",0) [color=red]strbearing=1*2[/color]
firstpt = SubString(strBearing, 0, pintcntr)
secondpt = SubString(strBearing, pintcntr+1,StrLength(strbearing)-pintcntr)
lineoftext=""
Do Until lineoftext = EOF
lineoftext=FileRead(c)   '[color=red] reads each line consecutively[/color]
coord()=StrSplit(lineoftext,",")
If coord(0)= secondpt Then  '[color=red] first character in the line of text [/color]
dblnorth2=coord(1) 
dbleast2=coord(2)
Exit    '[color=red] exit the loop when coord(0)=2 (second pt)[/color]
End If
Loop
-
-
-

I have verified that the variable 'secondpt' has the value of 2 (I set a break point and observed it)

Here is the issue: The program loops through the lines of text and parses the first character (coord(0)) in the order it should but WILL NOT execute the next line of code when it compares 'secondpt' with 2. If I use the actual number 2, then it works. It has no problem with 'firstpt' which is 1 ( I didn't include the code for that). I looked for a 'trim' or 'strtrim' thinking that maybe a leading space was causing the problem but couldn't find one. My app will be using this method a lot so I need to solve the problem. Thanks for all the past support your have given.

Jim
 

jschuchert

Active Member
Licensed User
Longtime User
I found solution. If I use "int" before the variable, it works. Apparently this is the only type changing keyword. I thought everything was a variant and the program would 'type' it within its context.
 

agraham

Expert
Licensed User
Longtime User
It is not a typing issue, the real problem is an "off by one" here
B4X:
secondpt = SubString(strBearing, pintcntr+1,StrLength(strbearing)-pintcntr)
You are asking for one more character than exists in the string so SubString is padding it with an extra space at the end. The If is doing a string comparison between "2" and "2 " and failing.

Your Int() is transforming "2 " to "2", forcing any arithmetic operation will in fact make it work like "secondpt * 1". The real fix is to get the length right in the first place.
B4X:
secondpt = SubString(strBearing, pintcntr+1,StrLength(strbearing)-pintcntr[COLOR="Red"][B]-1[/B][/COLOR])
 

jschuchert

Active Member
Licensed User
Longtime User
Yes, that is true and I think the '0' index was confusing me. Here was my thinking: pintcntr=2 (because the '*' is the second character) but with 0 as the index it is 1. (too much use of "mid" in other programs) Strlength(strbearing) was 3 ... pintcntr+1 (2+1) was 3 also and "strlength(strbearing) - pintcntr(2)" = 1...hence secondpt would be the value of "2" if I had used the correct index.

Your "off by one" is accurate and a good catch. After your analysis, I did some checking on the lengths of points 1 and 2 when I assigned them by the substring method earlier and found the same problem with "2". As a matter of fact, the length of strbearing was 4 (1*2 ) because of that. Thanks again for your eagle eye and great help. I really value your opinions and expertise.

Jim
 
Top