none
Reading Data? RRS feed

  • Question

  • Hello ;)

    So I'm trying to recreate a kind of DB app with SmallBasic, now I have this:

     

    A["id"] = 0
    A["age"] = 13
    
    File.WriteContents("C:\Database.txt", A)
    
    B = File.ReadContents("C:\Database.txt")

     

    So, the B returns this 'id=0;age=13;'

    How can I read the data and put everything in an array? And no, I don't want to use the A array ;).


    • Edited by RaoulV Friday, September 23, 2011 11:09 PM
    Friday, September 23, 2011 11:08 PM

Answers

  • The arrays in SmallBasic are actually what is called a dictionary, which is a list of associated data, in your case "id" is linked to 0 and "age" is linked to 13.

    This is stored internally as the list 'id=0;age=13;'.  So having read the file B, you could do use the variable B as an array, for example:

    TextWindow.WriteLine(B["age"]) will print 13.

    The array format can actually be useful sometimes to set an array quickly on one line, such as:

    days = "1=Sunday;2=Monday;3=Tuesday;4=Wednesday;5=Thursday;6=Friday;7=Saturday;"

    can be used as:

    TextWindow.WriteLine(days[3])

    Friday, September 23, 2011 11:28 PM
    Moderator

All replies

  • The arrays in SmallBasic are actually what is called a dictionary, which is a list of associated data, in your case "id" is linked to 0 and "age" is linked to 13.

    This is stored internally as the list 'id=0;age=13;'.  So having read the file B, you could do use the variable B as an array, for example:

    TextWindow.WriteLine(B["age"]) will print 13.

    The array format can actually be useful sometimes to set an array quickly on one line, such as:

    days = "1=Sunday;2=Monday;3=Tuesday;4=Wednesday;5=Thursday;6=Friday;7=Saturday;"

    can be used as:

    TextWindow.WriteLine(days[3])

    Friday, September 23, 2011 11:28 PM
    Moderator
  • Thanks, this helped me alot.

    Another question, how do I add arguments to a Sub ?

    Saturday, September 24, 2011 9:56 AM
  • You can't! 

    All variables in SmallBasic are global scope - that is they are available to be used and modified anywhere in your program.  This is for simplicity, but can cause confusion.

    There have been many discussions on this and different opinions and several approaches to passing arguments for example using a Stack, but my feeling is that this is a very clear rule - run with it and don't fight it.

    If you have a specific question about how to tackle a particular subroutine problem, then ask.

    Saturday, September 24, 2011 11:31 AM
    Moderator
  • But you can try this as a replacement for arguments:

    subWriteName_Text="Hello, "

    subWriteName_Name="AirWaves"

    WriteName()

    Sub WriteName

        TextWindow.WriteLine(subWriteName_Text + subWriteName_Name)

    EndSub


    ~~AirWaves!!~~
    Saturday, September 24, 2011 8:12 PM