Coders Block

Cableguy

Expert
Licensed User
Hi guys,

If there is such a thing as a writers Block, I'm having a Coders Block...
Been trying for hours to figure out how to declare an empty string array, to no avail...

HELP(!?!):sign0161:
 
Last edited:

Cableguy

Expert
Licensed User
I also suposed that would work, but when I use:
MyArray(0)="something"

I get an "undeclared Array" error, or am I Not incrementing the array length?
 

Cableguy

Expert
Licensed User
To make the subject more concrete....

I have an Array of strings that should be initialized as empty.
Then I want to read a text file, and add each line contents to the Array...So i need to increment the array manually, correct?
HOW?
 

LineCutter

Active Member
Licensed User
You have to dim the array first in globals

From the help (press F1 :))
Array
The Array keyword is used to initialize arrays and structures with the specified data.
It supports arrays of one and two dimensions and array structures of one dimension.
Arrays and structures should first be declared in Sub Globals.
However the size of the array can be set to 0 as the Array keyword will reinitialize the array.
Syntax: Array (Array Elements)
This might help too:
STRSPLIT
StrSplitSplits a string and returns an array of strings.
Syntax: SrtSplit (RawString, Separators)
RawString - The string that will be split.
Separators - Zero or more characters that act as the separators.
If the Separators string is an empty string then all white characters will be used as the separators.
Example:
Sub Globals
dim words(0) as string
End Sub
Sub App_Start
sentence = "$GPGGA,161229.487,3723.2475,N,12158.3416,W,1,07,1.0,9.0,M,,,,0000*18"
words() = StrSplit(sentence, ",")
for i = 0 to ArrayLen(words())-1
msgbox(words(i))
next
End Sub
 
Last edited:

Cableguy

Expert
Licensed User
I have gone trought the help files, before I even posted...
The Strings method WILL be of service, but only to rearrange the read strings from the file...
Since I have yet to know how many lines the file will contain, I was looking for a way to increment the array with every line read...
And I think I have just figured out, while writing this post...
All I need to code is:
MyArray()=FileRead(c1)
:sign0161:wasn't that too obvious?:sign0148::sign0013::sign0188:
 

Cableguy

Expert
Licensed User
Still no results....
Here's my code so far.

B4X:
Sub Globals
Dim Texto(0) as String
End Sub

Sub App_Start
ReadINI
Main.Show
MsgBox(Texto(3))'should show the 3rd line read from the file(?)
End Sub

Sub ReadNI
FileOpen(c1,AppPath & "\MyFile.txt",cRead")
a = FileRead(c!)
Do Until a = EOF
Texto() = a'Shouldn't this increment the array?
a = FileRead(c1)
Loop
FileClose(c1)
End Sub
*This code was all inputed by hand , as the actual coding was done in the device, so some typos may occour, but the code in the device is acurate.
 

LineCutter

Active Member
Licensed User
Try this:
B4X:
Sub Globals
    'Declare the global variables here.
    Dim txt(0) As string
End Sub

Sub App_Start
    readINI
    Msgbox(txt(0))
End Sub

Sub READINI
    FileOpen(c1,AppPath&"\MyFile.txt",cRead)
    a = FileReadToEnd(c1)
    txt()=StrSplit(a,crlf)
    FileClose(c1)
End Sub
 

Cableguy

Expert
Licensed User
IT WORKS:sign0060:
thanks Linecutter

Now I just have to trim the resulting strings...
All string have the same separator, a equal sign (=)...
so I need to eleminate all characters up to the equal sing, inclusive...
Any Ideas?
I was going for a For...next loop, going through the hole array, and trim each item, but I'm afraid that it would be too time/resource consuming..
the file will never(?) exceed the 60 lines...

EDIT:
LineCutter, your solution does provide all the read lines, but it also adds an empty "line" at every split, so I end up with an array with twice as much items than there really are...
But its workable...
Just have to figure a way to delete them...
Maybe I'll read one line at a time, but concatenate them into a single string and use the srtsplit method....
What do you think?
 
Last edited:

LineCutter

Active Member
Licensed User
LineCutter, your solution does provide all the read lines, but it also adds an empty "line" at every split, so I end up with an array with twice as much items than there really are...
That seems to be a "feature", presumably CR & LF are being read as separate chars in the matching. Doubtless Erel will point something out for us in a minute...
Maybe I'll read one line at a time, but concatenate them into a single string and use the srtsplit method....
What do you think?
That ought to work, although it offends my sense of economy.

BTW, since this looks much like INI file handling, have you thought about using the INI library?
 

Cableguy

Expert
Licensed User
YES, this is an INI file, and I did have knowleage of the first INI handling Code from DB2000, but didn't noticed that it had been turned to a DLL...
Thanks for pointing this out fo me...
I will see if it suites my simple needs...if not, I'll stick with the file reading...
 

Erel

Administrator
Staff member
Licensed User
StrSplit uses single characters to split the string.
If you write StrSplit(text,"abc") then it will 'a','b' and 'c' as the separators.
CRLF is actually Chr(13) and Chr(10).
This causes StrSplit to return Chr(10) after every line.
The solution is something like:
B4X:
Sub Globals
    'Declare the global variables here.
    Dim txt(0) As string
End Sub

Sub App_Start
    readINI
    For i = 0 To ArrayLen(txt())-1 [B]Step 2[/B]
        ListBox1.Add(txt(i))
    Next
    Form1.Show
End Sub

Sub READINI
    FileOpen(c1,AppPath&"\MyFile.txt",cRead)
    a = FileReadToEnd(c1)
    txt()=StrSplit(a,crlf)
    FileClose(c1)
End Sub
 

derez

Expert
Licensed User
The way I read text files withunknown length is by creating arraylists :

FileOpen(c53,trailfile,cRead)
st = FileRead(c53)
last_trail = 0
Do While st <> EOF
last_trail = last_trail + 1
record() = StrSplit(st,",")
trailgeolat.Add(record.a)
trailgeolong.Add(record.b)
trailutmx.Add(record.c)
trailutmy.Add(record.d)
st = FileRead(c53)
Loop
FileClose(c53)
 

roundi

New Member
Hello, Another way to read the ini file is to use StrReplace

B4X:
Sub App_Start
    readINI
    For i = 0 To ArrayLen(txt())-1
        ListBox1.Add(txt(i))
    Next
    Form1.Show
End Sub

Sub READINI
    FileOpen(c1,AppPath&"\MyFile.txt",cRead)
    a = FileReadToEnd(c1)
    a = StrReplace(a,crlf,Chr(10))
    txt() = StrSplit(a,Chr(10))
    FileClose(c1)
End Sub
Also in case you want to read a parameters in the ini file (eg. COMPORT=8), you can use the below sub to get the value of the COMPORT parameter:
port = GetValue("COMPORT")

B4X:
Sub GetValue(ParamName)

    Dim arrCount , sLine , sVal , i
   
    sVal = ""
    arrCount = ArrayLen(txt())
   
    If arrCount > 0 Then

        For i = 0 To arrCount-1

            sLine = txt(i)
            sline = StrReplace(sline," ", "") 'remove spaces
         
            If sLine <> "" Then
                pos = StrIndexOf(sline,ParamName & "=",0)
                If pos <> -1 Then
                    'remove parameter name and the = sign
               sline = StrReplace(sline,ParamName & "=", "") 
                    sVal = sline
                End If         
            End If
      
        Next

    End If
      
    Return sVal
   
End Sub
 

Cableguy

Expert
Licensed User
Ben there, Done that...But thanks anyway...
Always good to have others showing their aproaches to a problem...
 
Top