Results 1 to 6 of 6

Thread: Variable Subscripts

  1. #1

    Variable Subscripts

    While reading the documentation on the DIM statement, I noticed that the syntax allows for optional "subscripts" after 'VarName'. Here's what it says:

    DIM VarName[(subscripts)] AS VarType [AT Address] [ {VALUE | = | VALUE =} InitialValue ]
    
    So is the purpose of this to allow a series of variables, differentiated by a numerical subscript, to be declared at once? If so, what is the syntax for that? Should they be listed one by one, as in 'VarName(1, 2, 3) or can we use a range, as in 'VarName(1-3)'?

    I will try to experiment with it, but I ask it here, in case someone in the future is as ignorant as I am.

    By the way, MERRY CHRISTMAS everyone! Thanks for being here!

  2. #2

    It's for dimensioning arrays

    Alright, I found a little bit of my answer here:

    https://www.qbasic.net/en/reference/...tement/DIM.htm

    The syntax is still a little bit unclear to me, but I'll go back and look at the examples again. A lot of my confusion is a result of spending most of my time in Logo and Scheme. Logo (and maybe Scheme, can't remember) has arrays, but most Logo programmers don't use them, because the main data types are words and lists, not arrays. So I really need to spend some serious time getting comfortable with arrays. I remember the "Real Programmer" saying that linked lists were not necessary since they were just a special case of the array, but I think he didn't truly understand that (lisp) lists can have lists as elements, and you never have to "dimension" anything...that's all handled by the man behind the curtain.

    OK, I'll see y'all next time I have a DUMB question!

    Benjamin

  3. #3

    lower bound, upper bound

    In the one example given in the above link I shared, the 'DIM' statement says:

    DIM A(49, 49)

    Which I take as meaning "Declare an array "A" with a dimension of (0 TO 49)x(0 TO 49), in other words, a two-dimensional array with 50 "slots" in each dimension, with the index beginning at zero. So A(49,49) is taking advantage of the default behavior of qbasic, which is to dimension arrays beginning at index zero, unless specified. So the above is a shorthand way of saying 'A(0 TO 49, 0 TO 49)'

    From what I understand, thinBasic's arrays are indexed beginning at 1, so the equivalent in thinBasic would be 'DIM A(50, 50)'. I don't know if thinBasic's DIM statement can use the TO keyword referenced above. Perhaps someone could tell me. It appears from the wording of the documentation, that all thinBasic arrays are dimensioned beginning at index 1.

    If any of this is wrong, please correct me.

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,146
    Rep Power
    735
    EDIT: Wrote this as reaction to your first message, did not see the other 2. You are correct, thinBasic arrays are 1 based by default. We may consider adding different base in the future, but there is no ETA.

    Merry Christmas, Benjamin!,

    the note about subscripts is related to so called arrays.

    Arrays are great way to pack multiple items of the same type together, while allowing better flexibility than normal variables.

    One dimensional array
    Should you want to store names of your 3 friends and then print each, you can do this:
    USES "Console"
    
    ' Initialize array with values
    DIM friends(3) AS STRING = "Alex", "Bill", "Cecile"
    
    ' Change your mind about second friend:
    friends(2) = "Benjamin"  ' Changes "Bill" to "Benjamin"
    
    DIM i AS LONG
    
    FOR i = 1 to countOf(friends)
      PrintL friends(i)
    NEXT
    
    WAITKEY
    
    Anytime you want to increase number of friends you just increase the number to new number (for example 5) and the code will still work.

    If you would have just variables friend1, friend2, friend3 - it would be much harder to maintain the code.

    Please note the subscript is always 1 based at the moment. So specifying 3 as subscript means having friends(1), friends(2) and friends(3) created for you.

    CountOf is a great function to return number of items in 1 dimensional array.

    Two and more dimensional arrays
    You are not restricted to just 1 dimensional arrays, but you can use 2 dimensional, 3 dimensional.

    Here is again a little example:
    USES "Console"
    
    
    ' Initialize Tic-Tac-Toe game with empty cells
    DIM ticTacToe(5, 4) AS STRING = " ", " ", " ", " ", " ",
                                    " ", " ", " ", " ", " ",
                                    " ", " ", " ", " ", " ",
                                    " ", " ", " ", " ", " "
    
    
    ' Write initial player 1 move                                
    ticTacToe(2, 3) = "x"
    ' Write initial player 2 move
    ticTacToe(3, 3) = "o"
    
    
    DIM column, row AS LONG
    Dim singleCell aS STRING 
    
    
    ' Iterate through all cells in the array to draw Tic-Tac-Toe board
    FOR row = 1 to CountOf(ticTacToe, 2)        ' Count of items in second array dimension
      FOR column = 1 to CountOf(ticTacToe, 1)   ' Count of items in first array dimension
        ' Draw single cell
        singleCell = " " + ticTacToe(column, row) + " |"
        Print singleCell
      next
      
      ' Draw row separator
      printl
      printl repeat$(CountOf(ticTacToe, 1) * len(singleCell), "-")
    next
    
    
    WAITKEY
    
    Hope this helps to get some initial inspiration. One more tip - have a look in the help file at ARRAY statements. They can help you with some common tasks such as sorting, shuffling and more.


    Petr
    Last edited by Petr Schreiber; 24-12-2023 at 22:59.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5

    Adding more friends

    "Anytime you want to increase number of friends you just increase the number to new number (for example 5) and the code will still work."

    When you say "increase number of friends" do you mean use the REDIM statement?

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,146
    Rep Power
    735
    Hi Benjamin,

    Option 1 - you have some simple script when the amount of items is not dependant on user input
    You can simply alter 3 to 5 in this case, and you can specify the additional names

    Option 2 - dynamic array length manipulation
    In case you have some user interface, manipulating the array at run-time, then yes - REDIM, or most likely better REDIM PRESERVE can do the job for you

    Usage of TO, 1 based...
    Current versions of thinBASIC do not allow usage of TO clause, and all arrays are 1 based.

    DIM items(10) AS STRING ' CORRECT, will create array of 10 elements, indexed 1 - 10
    
    STRING items(10) ' CORRECT, will create array of 10 elements, indexed 1 - 10 - JUST SHORTER SYNTAX
    
    DIM items(1 TO 10) AS STRING ' Not supported at the moment
    
    Your mention of Linked Lists...
    Should you prefer Linked Lists for your project, thinBasic does provide implementation for you:
    https://help.thinbasic.com/linked_list.htm


    Petr
    Last edited by Petr Schreiber; 26-12-2023 at 00:03.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. help please, variable definition
    By DirectuX in forum OOP
    Replies: 4
    Last Post: 10-02-2024, 08:24
  2. New in next thinBasic: FOR with on-the-fly variable definition
    By ErosOlmi in forum thinBasic vaporware
    Replies: 16
    Last Post: 09-07-2008, 09:39

Members who have read this thread: 1

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •