IGNORED

# A few questions about arrays in Atari BASIC

## Recommended Posts

This is a modified snippet of some Atari BASIC code I'm looking at and I don't understand 2 aspects of it

```DIM B\$(1536)

B\$="B"
B\$(1536)="B"
B\$(2)=B\$

FOR J=0 TO 1280 STEP 256
B\$(45+J,45+J)="O"
NEXT J```

The first thing I don't get is the use of B\$ without an index, does this just default to B\$(0)?  same goes for the line that does B\$(2)=B\$, is that just shorthand for B\$(2)=B\$(0)?

The second thing is the line that indexes a 1d array as a 2d array, B\$(45+J,45+J)="O".   What is that doing?

Edited by telengard
##### Share on other sites

2 hours ago, telengard said:

This is a modified snippet of some Atari BASIC code I'm looking at and I don't understand 2 aspects of it

```DIM B\$(1536)

B\$="B"
B\$(1536)="B"
B\$(2)=B\$

FOR J=0 TO 1280 STEP 256
B\$(45+J,45+J)="O"
NEXT J```

The first thing I don't get is the use of B\$ without an index, does this just default to B\$(0)?  same goes for the line that does B\$(2)=B\$, is that just shorthand for B\$(2)=B\$(0)?

The second thing is the line that indexes a 1d array as a 2d array, B\$(45+J,45+J)="O".   What is that doing?

No. Atari only has statically sized strings, so the DIM just reserves a string of a maximum capacity of 1536 bytes. B\$="B" just sets this string to the value "B".  String indices in Atari basic create or address parts of the string, similar to what LEFT\$, MID\$ and RIGHT\$ do for other Basic dialects. B\$(X) is the substring starting at offset X, thus B\$(1536)="B" sets the last character of the entire string to "B", and thus ensures that the string is actually 1536 characters long, leaving the characters at indices 2 to 1535 undefined. Finally, B\$(2)=B\$ copies the string to the substring starting at offset 2, thus moves the character at offset 1 to the offset 2, then the character at offset 2 to offset 3 and so on, until the entire string is copied. Thus, it fills the entire string with "B"s.

In short, B\$(X) is the substring starting at X, and B\$(X,Y) the substring from offset X to Y, which both works at left and right side of an assignment. The drawback is that Atari Basic does not have string arrays.

Edited by thorfdbg
• 1
##### Share on other sites

9 minutes ago, thorfdbg said:

No. Atari only has statically sized strings, so the DIM just reserves a string of a maximum capacity of 1536 bytes. B\$="B" just sets this string to the value "B".  String indices in Atari basic create or address parts of the string, similar to what LEFT\$, MID\$ and RIGHT\$ do for other Basic dialects. B\$(X) is the substring starting at offset X, thus B\$(1536)="B" sets the last character of the entire string to "B", and thus ensures that the string is actually 1536 characters long, leaving the characters at indices 2 to 1535 undefined. Finally, B\$(2)=B\$ copies the string to the substring starting at offset 2, thus moves the character at offset 1 to the offset 2, then the character at offset 2 to offset 3 and so on, until the entire string is copied. Thus, it fills the entire string with "B"s.

In short, B\$(X) is the substring starting at X, and B\$(X,Y) the substring from offset X to Y, which both works at left and right side of an assignment. The drawback is that Atari Basic does not have strings.

Thank you!  So it's not an array, just a non default sized string.  Makes sense.  If I'm understanding correctly, the x,y indexing having the same value for x,y is just setting a particular index to the value?

##### Share on other sites

In the above, I am using X and Y only as placeholders. They could be avariables, or arbitrary (numeric) expressions.

##### Share on other sites

1 hour ago, telengard said:

The first thing I don't get is the use of B\$ without an index, does this just default to B\$(0)?

There is no position 0, so B\$(1) is the same as B\$, i.e. the substring staring at position 1 of B\$ up to its full current lenght.

36 minutes ago, telengard said:

If I'm understanding correctly, the x,y indexing having the same value for x,y is just setting a particular index to the value?

Using both indexes when assigning a value prevents the string to be resized to that index plus the length of the assigneed value.

```DIM A\$(100)
A\$="12346789"
A\$(5,5)="X"
PRINT A\$
A\$(5)="Y"
PRINT A\$```

The first result is 1234X6789, while the second is just 1234Y and the final length of the string is only 5 chars.

• 1
##### Share on other sites

1 hour ago, thorfdbg said:

The drawback is that Atari Basic does not have strings.

?

• 1

##### Share on other sites

String arrays start at element 1, not 0.

Value assignment works differently if you use A\$(x,y) vs A\$(X)

If you use A\$(x,y) = "b" the string length will be preserved if it was longer than y.

If you use A\$(x) = "b" the string length will terminate at the end of "b"  - the string won't be allowed to grow bigger than it's DIM value though.

For initial filling you can use a shortcut, no need to populate the end character to be filled.  e.g.

DIM A\$(10)

A\$="0" : A\$(10)="" : A\$(2)=A\$

## 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. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.