locked
Arrays in Small Basic RRS feed

  • Question

  • I'm looking for a source that can explain how to connect and convert a few arrays (mostly one-dimensional -- integer and character) from the old QuickBasic format to Small Basic, I'm confused by the lack of DIM(ension) statements, and the designation of String(character) data; (I understand the numbering of components from 0-9 instead of 1-10, FWIW), 

    Can anyone point me in the direction of a Web-based reference where these issues are addressed? Thanks!



    • Edited by Eddie Sand Saturday, August 11, 2018 4:52 PM
    Friday, August 10, 2018 6:53 PM

Answers

  • sb arrays are dinamic and also slow. if u need a fast array types, then u have ldmatrix, ldarray and ldfastarray from ld xtension
    standard sb arrays use square brackets:

    1d array: a[1]=2

    2d array: a[2][3]=4

    3d array: a[3][4][5]=6

    u can also use logic constants true/false, useful for NOT logical operation

    not="false=true;true=false"

    sb online uses another type of arrays which can have only numerical indexes,and cannot be declared as NOT above,only with square brackets

    another obsolette and oldstyle method is to use array.setvalue and array.getvalue, but u can't combine it with arrays above, if u write a[2]=5 then u cannot use array.getvalue(a, 2) to retrieve it. the advantage of this method is that it's faster than sb array and also u can have many same or similar arrays, just change name to do ops on them.

    u can have array also with ldtext.split command which converts string to array, very useful to compact things and read command lines. just type a=ldtext.split("quick brown fox", " ") and u got an array of 3 words. it also skips empty spaces and lines.

    also a good practice is to initiate arrays with FOR clause, bcs sometimes u get wrong results if u dont do so, especially with multidim. arrays

    for x=1 to 20
       for y=1 to 30
         a[x][y]=0
       endfor
    endfor

    other issue is if u use large arrays it takes an eternity to read them, so don't use more than 5000 elements, or use ld arrays instead





    • Edited by Tryhest Saturday, August 11, 2018 3:57 AM
    • Marked as answer by Eddie Sand Saturday, August 11, 2018 3:28 PM
    Saturday, August 11, 2018 3:36 AM
  • Do you have these data in a textfile (e.g. comma seperated)?

    Look at the File.ReadLine command. Then you have to split the line in de eight numbers with e.g. Text.GetSubText.

    How to use these commands is shown in the "Intellisense window".


    Jan [ WhTurner ] The Netherlands

    • Marked as answer by Eddie Sand Sunday, August 26, 2018 3:23 PM
    Sunday, August 26, 2018 2:40 PM
    Answerer

All replies

  • Here is a wiki article on Small Basic Arrays.

    There are no "types" in Small Basic, everthing is of type PRimitive that can be decimal, string and array.

    Arrays are dynamic so no dimensioning required - they are internally .Net Dictionaries that can also be represented by a string with indices and elements deliminated by = and ;

    data[1] "test"
    data["X"] = 3

    can also be set as

    data = "1=test;X=3;
    Perhaps give a short example of QBasic code you want to convert or ask specific question to clarify above.

    Friday, August 10, 2018 7:35 PM
  • sb arrays are dinamic and also slow. if u need a fast array types, then u have ldmatrix, ldarray and ldfastarray from ld xtension
    standard sb arrays use square brackets:

    1d array: a[1]=2

    2d array: a[2][3]=4

    3d array: a[3][4][5]=6

    u can also use logic constants true/false, useful for NOT logical operation

    not="false=true;true=false"

    sb online uses another type of arrays which can have only numerical indexes,and cannot be declared as NOT above,only with square brackets

    another obsolette and oldstyle method is to use array.setvalue and array.getvalue, but u can't combine it with arrays above, if u write a[2]=5 then u cannot use array.getvalue(a, 2) to retrieve it. the advantage of this method is that it's faster than sb array and also u can have many same or similar arrays, just change name to do ops on them.

    u can have array also with ldtext.split command which converts string to array, very useful to compact things and read command lines. just type a=ldtext.split("quick brown fox", " ") and u got an array of 3 words. it also skips empty spaces and lines.

    also a good practice is to initiate arrays with FOR clause, bcs sometimes u get wrong results if u dont do so, especially with multidim. arrays

    for x=1 to 20
       for y=1 to 30
         a[x][y]=0
       endfor
    endfor

    other issue is if u use large arrays it takes an eternity to read them, so don't use more than 5000 elements, or use ld arrays instead





    • Edited by Tryhest Saturday, August 11, 2018 3:57 AM
    • Marked as answer by Eddie Sand Saturday, August 11, 2018 3:28 PM
    Saturday, August 11, 2018 3:36 AM
  • Thanks for the help so far; the original program was a "routing model" for the calculation of mileage to compensate drivers; it was built around identifiable cities/towns (large and small), and some of these had more than one pickup/delivery point; some of these were also "satellites" linked to more heavily-used "hub" cities, so the format was:

    Dim LOCN(4,100)

    where the location's ID number, status, alphabetic code and (if applicable) Hub point(character data) were stored and referenced.

    Example:

    1,H,SCRN,** (Scranton)

    2,H,ALTN,**  (Allentown)

    3,S,QKTN,AL (Quakertown-Allentown satellite)

    4,X,BLOM,** (Bloomsburg)

    5,S,PTST,SC (Pittston-Scranton sateilite)

    where H=Hub; S=Satellite, and X=extraneous point not linked with any other point

    Larger cities might also have more than one point under the same alpha-code, and some points in the original array were left "null" for later use; the itinerary was entered, and total mileages calculated, using component mileages previously entered in a two-dimensional array.

    As the model grew (greater distances and approaches to a hub from more than one direction or "gateway"), the variations and options (too complex to get into here) increased.

    And as with another algorithm I'd developed to simulate rail dispatching on single track, there were points where the data overwhelmed the process; but this could usually be addressed by diminishing the system's size and complexity, or by manually entering a temporary fix, and continuing the principal process.

    At this stage, I'm just looking for a starting point; Thanks!










    • Edited by Eddie Sand Saturday, August 11, 2018 4:49 PM
    Saturday, August 11, 2018 3:56 PM
  • This can be dome in SB using muti-diemnsional arrays.  Note that this can be slow if there are lots of then (100 or so should be fine).

    Many ways the data can be used or set up.  You would probably want to first choose some format you can get the data into a file to read into SB.

    Here is an example of one way the data can be stored - there may be better depending on how it is to be used, sorted cross referenced etc.

    Other more complex possibilities using extensions e.g. using LD arrays (for large data sets) or even simple databases (SQLite - for complex sorting, finding subset, cross refences etc using SQL).

    data["H"]["SCRN"] = "Scranton"
    data["H"]["ALTN"] = "Allentown"
    data["S"]["QKTN"] = "Quakertown,Allentown,satellite"
    data["X"]["BLOM"] = "Bloomsburg"
    data["S"]["PTST"] = "Pittston,Scranton,sateilite"
    
    indicesID = Array.GetAllIndices(data)
    For i = 1 To Array.GetItemCount(indicesID)
      ID = indicesID[i]
      dataStatus = data[ID]
      indicesStatus = Array.GetAllIndices(dataStatus)
      For j = 1 To Array.GetItemCount(indicesStatus)
        Status = indicesStatus[j]
        dataCode = dataStatus[Status]
        pos = Text.GetIndexOf(dataCode,",")
        While (pos > 0)
          Code = Text.GetSubText(dataCode,1,pos-1)
          dataCode = Text.GetSubTextToEnd(dataCode,pos+1)
          pos = Text.GetIndexOf(dataCode,",")
          TextWindow.WriteLine("ID="+ID+ " Status="+Status+" Code="+Code)
        EndWhile
        Code = dataCode
        TextWindow.WriteLine("ID="+ID+ " Status="+Status+" Code="+Code)
      EndFor
    EndFor

    Saturday, August 11, 2018 4:50 PM
  • use ld sqlite dbase if there are many data to analyse. also u have lddatagrid for easier data manipulation and editing. once u made data into dbase u just use sql statements, which can gain high complexity but the principle is always same: SELECT (what, or * for all) FROM (table) WHERE (conditions) GROUP BY (if u want groups) HAVING (group conditions) ORDER BY (column ASC or DESCending). more about sqlite dbase on sqlite web:

    http://zetcode.com/db/sqlite for SQLite commands


    • Edited by Tryhest Saturday, August 11, 2018 8:08 PM
    Saturday, August 11, 2018 8:07 PM
  • OK; I'm trying to initialize a two-dimensional (8x4) array using two sets of nested For/EndFor statements as depicted below:

    Np=8
    for j=1 to Np
       for k=1 to 4
       Locn[j,k]=0
       endfor
    endfor
    TextWindow.Writeline (Locn[6,4])

    The error messages make reference to an Expected Right Bracket at the line "Locn[j,k]=0" but I can see no reason for this; Any suggestions welcome.


    Tuesday, August 14, 2018 1:51 PM
  • You need to use  Locn[j][k]=0

    and   TextWindow.Writeline (Locn[6][4])

    See also previous answers


    Jan [ WhTurner ] The Netherlands




    Tuesday, August 14, 2018 3:08 PM
    Answerer
  • Thanks, and apologies for overlooking the "obvious" (which, I suspect, happens a lot in this environment; a long absence makes for a lot of "rust").
    Tuesday, August 14, 2018 5:26 PM
  • Thanks for the help so far; I have a table of mileages used for reference (cut down for now to an 8x8 grid) which I want to incorporate into the routing model as a data reference (not all of the 64 possible combinations will be  relevant) But I haven't been able to read/enter thus data into the SmallBASIC program. 
    Sunday, August 26, 2018 2:21 PM
  • Do you have these data in a textfile (e.g. comma seperated)?

    Look at the File.ReadLine command. Then you have to split the line in de eight numbers with e.g. Text.GetSubText.

    How to use these commands is shown in the "Intellisense window".


    Jan [ WhTurner ] The Netherlands

    • Marked as answer by Eddie Sand Sunday, August 26, 2018 3:23 PM
    Sunday, August 26, 2018 2:40 PM
    Answerer
  • Perhaps, first create a csv (comma separted value) file - can be easily exported from excel.

    The first row should hold the number of towns followed by the town names and thenn the array of distances.  It might look a bit like (for 3 towns).  I'm assuming distance from town1 to town1 is 0 and between town1 and town2 is the same as between town2 and town1, so the matrix is diagonal 0 and symetric.

    3
    town1
    town2
    town3
    0,63,42
    63,0,22
    42,22,0

    Then you need to read this data file into SB.

    fileName = Program.Directory+"\millages.txt"
    numTown = File.ReadLine(fileName,1)
    For i = 1 To numTown
      town[i] = File.ReadLine(fileName,1+i)
    EndFor
    For i = 1 To numTown
      data = File.ReadLine(fileName,1+numTown+i)
      For j = 1 To numTown
        pos = Text.GetIndexOf(data,",")
        If (pos > 0) Then
          millages[i][j] = Text.GetSubText(data,1,pos-1)
          data = Text.GetSubTextToEnd(data,pos+1)
        Else
          millages[i][j] = data
        EndIf
      EndFor
    EndFor
    
    For i = 1 To numTown
      TextWindow.Write(town[i]+" ")
      For j = 1 To numTown
        TextWindow.Write(millages[i][j]+" ")
      EndFor
      TextWindow.WriteLine("")
    EndFor
    


    Sunday, August 26, 2018 2:59 PM
  • Well, thanks again to all who've helped me I think I have a plan.

    The routing model used three sets of information:

    A list of "points" where pickups/deliveries were made

    A (considerably-) smaller list of "hubs" from which alternate/diverging routes could be chosen

    (these first two had several other characteristics listed within a two-dimensional array)

    and a tale of mileages (also presented as a two dimensional array)

    ALL OF THESE ITEMS WERE STATIC, AND NOT SUBJECT TO CHANGE ONCE THE MODEL WAS SET UP, (Save on rare occasions).

    So it would seem that the obvious answer is to store these values as files (probably with simple comma-separated input)

    I have very little experience inworking with files (cold use some reference and will probably be back with a few more questions.




    • Edited by Eddie Sand Thursday, August 30, 2018 12:08 AM
    Thursday, August 30, 2018 12:00 AM