Coders Block

Discussion in 'Questions (Windows Mobile)' started by Cableguy, May 26, 2008.

  1. Cableguy

    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: May 26, 2008
  2. LineCutter

    LineCutter Active Member Licensed User

    Dim MyArray(0) as string ??
     
  3. Cableguy

    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?
     
  4. Cableguy

    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?
     
  5. LineCutter

    LineCutter Active Member Licensed User

    You have to dim the array first in globals

    From the help (press F1 :))
    This might help too:
    STRSPLIT
     
    Last edited: May 26, 2008
  6. Cableguy

    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:
     
  7. Cableguy

    Cableguy Expert Licensed User

    Still no results....
    Here's my code so far.

    Code:
    Sub Globals
    Dim Texto(0as 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.
     
  8. LineCutter

    LineCutter Active Member Licensed User

    Try this:
    Code:
    Sub Globals
        
    'Declare the global variables here.
        Dim txt(0As 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
     
  9. Cableguy

    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: May 27, 2008
  10. LineCutter

    LineCutter Active Member Licensed User

    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...
    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?
     
  11. Cableguy

    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...
     
  12. Erel

    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:
    Code:
    Sub Globals
        
    'Declare the global variables here.
        Dim txt(0As 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
     
  13. derez

    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)
     
  14. LineCutter

    LineCutter Active Member Licensed User

    I knew I was missing something obvious (I usually do). Thanks Erel.
     
  15. roundi

    roundi New Member

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

    Code:
    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")

    Code:
    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
     
  16. Cableguy

    Cableguy Expert Licensed User

    Ben there, Done that...But thanks anyway...
    Always good to have others showing their aproaches to a problem...
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice