Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: brucem on March 31, 2004, 10:37:30 PM



Title: Access to data
Post by: brucem on March 31, 2004, 10:37:30 PM
The last time I programmed was with GW Basic, over ten years ago. With that program, I was able to use a matrix format to store information that I could use repeatedly, modifying it each time a little bit, in one run. If I remember right, I had to dimension the size of the matrix first, and then I could set up a counter that would locate a specific piece of data in the matrix. How is this done in QBasic? I have just started using QBasic and am just beginning to learn the ropes. (The matrix size I used before was 60 x 60)

Thanks for any help!
--Bruce


Title: Access to data
Post by: Pyrodap on March 31, 2004, 11:24:01 PM
Code:
DECLARE SUB UpdateMatrix (FileName$)
DECLARE SUB LoadMatrix (FileName$)
DECLARE SUB MakeMatrix (FileName$)
DIM SHARED Matrix$(60, 60)


SUB LoadMatrix (FileName$)
        OPEN FileName$ FOR INPUT AS #1
        FOR y = 1 TO 60
        FOR x = 1 TO 60
        INPUT #1, Matrix$(x, y)
        NEXT
        NEXT
        CLOSE #1
END SUB

SUB UpdateMatrix (FileName$)
        OPEN FileName$ FOR OUTPUT AS #1
        FOR y = 1 TO 60
        FOR x = 1 TO 60
        m$ = m$ + Matrix$(x, y) + ","
        NEXT
        PRINT #1, LEFT$(m$, LEN(m$) - 1)
        m$ = ""
        NEXT
        CLOSE #1
END SUB

SUB MakeMatrix(FileName$)
        OPEN FileName$ FOR OUTPUT AS #1
        FOR y = 1 TO 60
        FOR x = 1 TO 60
        m$ = m$ + "Empty!,"
        NEXT
        PRINT #1, LEFT$(m$, LEN(m$) - 1)
        m$ = ""
        NEXT
        CLOSE #1
END SUB



Okay! that's not too hard.

Before you do anything, you have to have a matrix created. Just call:
Code:
MakeMatrix "Matrix.txt"

Now remember, after a Matrix is created, if you call this sub again it will erase everything in the matrix. Once you have a matrix made, you have to load it.
Code:
LoadMatrix "Matrix.txt"

Once it is loaded, all you have to do to change a value is this:
Code:
Matrix$(10, 10) = "Yay!!"

That will change the value of the 10 by 10 of the matrix to "Yay!!" Now, even though it is changed in the program, it isn't saved. The last thing you do before you close the program is:
Code:
UpdateMatrix "Matrix.txt"

And that's it!

I hope this will suffice, but if not i'll be right here!