Jump to content
IGNORED

String array initializing in fast basic


Recommended Posts

I can't for the life of me figure out how to declare a string array and initialize it.

 

If I try something like this:

DIM strARR$(5)
strARR$() = "Hello", "this", "will", "not", "work", "to initalize"

This will not work naturally.

Alright I'll try a DATA statement:

DATA strArr() byte = "How","do","I","access","this?"
FOR I=1 TO 10
  PRINT strArr(I)
NEXT I

This will produce numerical output. Having the READ statement from the Atari basic is looking more attractive at this point.

 

So how does one use string arrays in Fast Basic?

Link to comment
Share on other sites

In your first statement, you are trying to set 6 different strings into a single string, maybe something like

DIM strARR$(5,12)       I assume Fast Basic arrays start at 0 so you have 6 strings each 13 bytes long to accommodate the largest string.

 

Same problem with the second method, you need a 2 dimensional array.

 

Not too sure about this initialising method as I don't use FB, but you definitely need 2 dimensional arrays to start with.

 

Edit: I also think you have to "DIM" the array first then initialise it afterwards.

e.g.

DIM a$(10,10)

a$(0)="hello"

a$(1)="world"

etc...

or have DATA statements and read them in in a for/loop.

Edited by TGB1718
Link to comment
Share on other sites

String variables in FastBasic are always of 255 bytes lenght, which uses 256 bytes in RAM (with a leading byte that stores the current length).

 

String arrays stores a list of addresses where each of the elements is stored.

 

To initialize a strings array, you must assign a value to each element, but you can include the list of strings in a DATA array:

 

DIM A$(10)

DATA B() BYTE = "Cero","Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve","Diez"

X = ADR(B)
FOR I = 0 TO 10
  A$(I) = $(X)
  X = X + PEEK(X) + 1
NEXT

FOR I = 0 TO 10
  ? A$(I), ADR(A$(I))
NEXT

 

 

 

 

Link to comment
Share on other sites

15 minutes ago, vitoco said:

To initialize a strings array, you must assign a value to each element, but you can include the list of strings in a DATA array:


DIM A$(10)

DATA B() BYTE = "Cero","Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve","Diez"

X = ADR(B)
FOR I = 0 TO 10
  A$(I) = $(X)
  X = X + PEEK(X) + 1
NEXT

FOR I = 0 TO 10
  ? A$(I), ADR(A$(I))
NEXT

 

 

If the values of the array would be constant during the program execution, there is a problem with this approach, because the data would use too much memory: the "packed" data in the B() array, and extra 256 bytes per element of every used array element (as it could be seen in the PRINT statement).

 

I could figure out a trick to save those pages of memory from the initialization:

DIM AA(-1), A$(10)

DATA B() BYTE = "Cero","Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve","Diez"

X = ADR(B)
FOR I = 0 TO 10
  AA(I) = X
  X = X + PEEK(X) + 1
NEXT

FOR I = 0 TO 10
  ? A$(I), ADR(A$(I))
NEXT

 

This trick assigns the same memory location for both the AA() WORD array and A$() string array (which it is a WORD array of pointers). So, assigning a memory location to AA(I) makes A$(I) point to a string that starts at that location (remember that the first byte is actually the string length). In this example, the PRINT shows the original memory location of the initialization data.

 

If you use this method, AVOID assigning new values to the elements of the string array, because those are pointers to data inside the bytecode area (the tokens of the compiled program), without a filler to complete 256 bytes per element, and the program will crash!!!

 

Link to comment
Share on other sites

Hi!

3 hours ago, vitoco said:

String variables in FastBasic are always of 255 bytes lenght, which uses 256 bytes in RAM (with a leading byte that stores the current length).

 

String arrays stores a list of addresses where each of the elements is stored.

 

To initialize a strings array, you must assign a value to each element, but you can include the list of strings in a DATA array:

 

3 hours ago, vitoco said:

DIM A$(10)

DATA B() BYTE = "Cero","Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve","Diez"

X = ADR(B)
FOR I = 0 TO 10
  A$(I) = $(X)
  X = X + PEEK(X) + 1
NEXT

FOR I = 0 TO 10
  ? A$(I), ADR(A$(I))
NEXT

 

I prefer this, it is more "idiomatic", as it does not uses PEEK:

DIM A$(10)

DATA B() BYTE = "Cero","Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve","Diez"

X = ADR(B)
FOR I = 0 TO 10
  A$(I) = $(X)
  X = X + LEN($(X)) + 1
NEXT

FOR I = 0 TO 10
  ? A$(I), ADR(A$(I))
NEXT

 

As Vitoco said, in the above example, FastBasic allocates 256 bytes for each string assignment (the "A$(I) = ...." in the loop), and copies the "constant" string from the DATA array to the new allocated string.

 

If your strings are always constants, and you don't want to use much memory, you can instead store the pointers (address) to the strings in a new array:

DIM A(10)
DATA B() BYTE = "Cero","Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve","Diez"

X = &B
FOR I = 0 TO 10
  A(I) = X
  X = X + LEN( $(X) ) + 1
NEXT

FOR I = 0 TO 10
  ? $( A(I) )
NEXT

 

Link to comment
Share on other sites

Thanks. This really clears things up. The manual seemed as clear as mud in regards to using string arrays, at least to me. Been working through "Your Atari Computer" translating the example programs to Fast Basic. I figure I'll really know the language if I can "port" every example. Strings though are done differently enough in Fast Basic compared to Atari Basic that it was getting frustrating. Again thanks.

Link to comment
Share on other sites

Hi!

7 hours ago, Quiver said:

Thanks. This really clears things up. The manual seemed as clear as mud in regards to using string arrays, at least to me.

Yes, the manual is not very deep in arrays and strings. Perhaps you can suggest some edits to make it better? I would appreciate it.

 

7 hours ago, Quiver said:

Been working through "Your Atari Computer" translating the example programs to Fast Basic. I figure I'll really know the language if I can "port" every example. Strings though are done differently enough in Fast Basic compared to Atari Basic that it was getting frustrating.

I decided to implement strings differently to make it easier to use strings in simple programs - without the need for DIM using strings is more natural, IMHO.

 

The main problem is that FastBasic string support is really simple, so it does not do garbage-collection like in other BASIC with automatic strings, so I opted for statically allocated strings - basically, whenever you perform a string assignment the code generated is:

' Code for A$="hello"
IF NOT ADR(A$) THEN DIM A$(256)
MOVE ADR("hello"), ADR(A$), LEN("hello") + 1

The advantage of statically allocates strings are:

- you can edit the contents and concatenate it in the same buffer (for example, you can do  A$ += "world" to append a word)

- you don't loose memory on each assignment

- the address is fixed, so you can store machine code or binary data inside.

 

The obvious disadvantages are:

- the string always use 256 bytes, even for short strings.

- you can't store "constant" strings in a string variable, as they can't be overwriten.

 

Have Fun!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...