1

What is a rule for converting an array into a string in Small Basic?

b[0] = "b0"
b[2] = 1

a[0][0] = "a"
a[0][1] = 123
a[1][2] = "True"
a[1][3] = b

TextWindow.WriteLine(a)
'0=0\=a\;1\=123\;;1=2\=True\;3\=0\\\=b0\\\;2\\\=1\\\;\;;

c[0][0][0] = "a"
c[0][1][0] = 123
c[1][2][0] = "True"
c[1][3][0] = "b0"
c[1][3][2] = 1

TextWindow.WriteLine(c)
'0=0\=0\\\=a\\\;\;1\=0\\\=123\\\;\;;1=2\=0\\\=True\\\;\;3\=0\\\=b0\\\;2\\\=1\\\;\;;

In the conversion examples above, I do not understand when backslashes come.

Could you please help me to understand the conversion rule?

Thanks in advance.

G.Kim

김가영
  • 11
  • 2

1 Answers1

1

In most programming languages, arrays are very efficient and are designed to run as quickly as possible. This is not the case with Small Basic. Actually, Small Basic does not, strictly speaking, have an array structure. Arrays are stored as a Map where a key is stored as a pair with its matching value.

You usually use arrays as such: ary[1] = 12

But you could just as easily use it as such: ary["cat"] = 12

The first versions of Small Basic didn't even support the square bracket [] notation. You stored lists in information using a Map object. For backwards compatibility the Map object is still there. Access to it has been wrapped up in some syntactic sugar to take it more traditional. The end result is something very flexible, but very slow.

Now, to answer your actual question...

What you are seeing is when you write out an array is the Map storage of your array. The values are in pairs. The map key, an equal sign, and the associated map value, ending with a semicolon. When you do multiple dimensional arrays the idea is nested. For a two dimensional array you get the first index, an equal sign, the second index, a slash, a second equal sign and then the associated value. For each added index you get another slash and another semicolon. This is easier to see when you use non-numeric indexes.

a["cat"] = "hat"
a["shark"] = "bait"

TextWindow.WriteLine(a)  'cat=hat;shark=bait;

b["apple"]["orange"] = "fruit"
b["VW"]["BMW"] = "cars"

TextWindow.WriteLine(b)  'apple=orange\=fruit\;;VW=BMW\=cars\;;

c["hot"]["medium"]["cold"] = "temps"
c["cheese"]["meat"]["vegies"] = "food"

TextWindow.WriteLine(c)  'hot=medium\=cold\\\=temps\\\;\;;cheese=meat\=vegies\\\=food\\\;\;;

The idea can go in reverse. If you want to make your code as compact as possible you can load an array using a string. Simply follow the pattern detailed above and rather then having six different lines to load six values into six indexes, you can do it all at once on a single line:

d = "work=money;stone=cold;"

TextWindow.WriteLine(d["work"]) 'money
TextWindow.WriteLine(d["stone"]) 'cold
codingCat
  • 2,396
  • 4
  • 21
  • 27