# Initialising arrays (and programming language implementation)

**URL:** <https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975>\
**Category:** ChoiceScript Help\
**Created:** [April 27, 2016, 8:01pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975 "2016-04-27T20:01:23Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![amca](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/amca/32/11290_2.png) [@amca](https://forum.choiceofgames.com/u/amca)\
**Post date:** [April 27, 2016, 8:01pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/1 "2016-04-27T20:01:23Z")

</div>

Hi.

I’ve been experimenting with how to simulating arrays. Is my understanding correct that you have to “manually” define each “array” index?

e.g.  
\*temp MyArray\_1  
\*temp MyArray\_2  
\*temp MyArray\_3  
\*temp MyArray\_4

Also, has someone else been insane enough to try and implement a (toy/esoteric) programming language in Choicescript and I’ve just missed it?

Andrew

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [April 27, 2016, 8:05pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/2 "2016-04-27T20:05:39Z")

</div>

Yes, currently you have to define “arrays” by hand, but can reference them as you normally would, i.e. myarray[1].

Do you mean like a scripting language ontop of Choicescript? Which is already on top of JavaScript. Not yet…

---

<div class="post-metadata">

**Author:** ![Nahim\_Kerman](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/nahim_kerman/32/62734_2.png) [@Nahim\_Kerman](https://forum.choiceofgames.com/u/Nahim_Kerman)\
**Post date:** [April 27, 2016, 8:58pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/3 "2016-04-27T20:58:09Z")

</div>

Excuse for my noobness but what exactly is an “Array” for?

---

<div class="post-metadata">

**Author:** ![amca](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/amca/32/11290_2.png) [@amca](https://forum.choiceofgames.com/u/amca)\
**Post date:** [April 27, 2016, 9:56pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/4 "2016-04-27T21:56:11Z")

</div>

An array is used to group together related objects, so that you can refer to them by a number, rather than separate names.

For example, if you wanted to hold a list of birds, instead of having to think up as many variable names as there are birds (the amount of which you may not know when you are writing your program), you can have an array with a whole bunch of birds and refer to them by number (array index), including a number in another variable.

Instead of  
\*create first\_bird “sparrow”  
\*create second\_bird “robin”  
\*create third\_bird “finch”  
you could do  
\*create\_array birds 3  
\*set birds[1] “sparrow”  
\*set birds[2] “robin”  
\*set birds[3] “finch”

It may seem in this example that arrays are pointless, but in more sophisticated programs they are very useful, especially when you use a variable for the index like  
\*input\_text birds[current\_bird]

Feel free to ask me any more questions or for clarification.

---

<div class="post-metadata">

**Author:** ![Kelvin](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/kelvin/32/8950_2.png) [@Kelvin](https://forum.choiceofgames.com/u/Kelvin)\
**Post date:** [April 27, 2016, 10:24pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/5 "2016-04-27T22:24:51Z")

</div>

Well I may be a regular on this forum but I have no idea how actual code works. @amca would you mind ellaborating more on how an array is useful? Also it seems like it would be a lot of work to keep track of which bird is bird[1], bird[2], etc. Is it?

---

<div class="post-metadata">

**Author:** ![amca](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/amca/32/11290_2.png) [@amca](https://forum.choiceofgames.com/u/amca)\
**Post date:** [April 27, 2016, 10:52pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/6 "2016-04-27T22:52:46Z")

</div>

There are two main types of situation where you would use such an array.

The first situation is where you don’t care what order they are in, you just want them all grouped together so they are easier to manipulate.

The second situation is where the index (number) actually means something. For instance they could be in order of favourite bird. So the sparrow would be the favourite bird, the robin the second favourite bird and the finch the least favourite bird.

---

<div class="post-metadata">

**Author:** ![P\_Tigras](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/p_tigras/32/626_2.png) [@P\_Tigras](https://forum.choiceofgames.com/u/P_Tigras)\
**Post date:** [April 27, 2016, 11:04pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/7 "2016-04-27T23:04:56Z")

</div>

From a story-writing perspective, arrays are especially useful for managing a character’s inventory.

---

<div class="post-metadata">

**Author:** ![amca](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/amca/32/11290_2.png) [@amca](https://forum.choiceofgames.com/u/amca)\
**Post date:** [April 27, 2016, 11:06pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/8 "2016-04-27T23:06:47Z")

</div>

Yep. That’s the sort of array where you don’t care where something is in an array, just whether it is or not.

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [April 27, 2016, 11:10pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/9 "2016-04-27T23:10:34Z")

</div>

Example of use:  
[http://pastebin.com/1Lx3CNKs](http://pastebin.com/1Lx3CNKs)

Though this is actually from before the addition of the [i] syntax, so it’d be even easier now.

---

<div class="post-metadata">

**Author:** ![stainedofmind](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/stainedofmind/32/459_2.png) [@stainedofmind](https://forum.choiceofgames.com/u/stainedofmind)\
**Post date:** [April 28, 2016, 6:45pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/10 "2016-04-28T18:45:15Z")

</div>

You can actually use looping code to declare \*temp arrays, but for \*create arrays, you’d have to define them manually. The reason one works, but the other doesn’t is due to the fact that all your \*create lines must be in the same block of code, and this breaks when you try to do anything else.

---

<div class="post-metadata">

**Author:** ![amca](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/amca/32/11290_2.png) [@amca](https://forum.choiceofgames.com/u/amca)\
**Post date:** [April 28, 2016, 6:59pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/11 "2016-04-28T18:59:20Z")

</div>

Thanks, I’ll try that. I think I tried it with \*create arrays, but it didn’t work, so I started a different approach.

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [April 28, 2016, 7:55pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/12 "2016-04-28T19:55:40Z")

</div>

Are you sure? Last I checked you couldn’t create \*temp or \*create variable names dynamically (i.e. use another variable to help define their name).

---

<div class="post-metadata">

**Author:** ![Nahim\_Kerman](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/nahim_kerman/32/62734_2.png) [@Nahim\_Kerman](https://forum.choiceofgames.com/u/Nahim_Kerman)\
**Post date:** [April 30, 2016, 5:03am UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/13 "2016-04-30T05:03:20Z")

</div>

Oh!

I fear i already tried to code something like this!

But i was using a string as a vector for my system since choicescript can “read” word’s lenght and “write” by silabic position.

---

<div class="post-metadata">

**Author:** ![stainedofmind](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/stainedofmind/32/459_2.png) [@stainedofmind](https://forum.choiceofgames.com/u/stainedofmind)\
**Post date:** [May 2, 2016, 4:45pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/14 "2016-05-02T16:45:33Z")

</div>

Pretty sure I remember trying out loops with \*temp. It’s been quite a while though, so I may either be mistaken, and I was just theorizing it could be done, or it’s been changed. I’ma have to test this out again either way.

Edit: Yup. I was wrong. My bad 😛

---

<div class="post-metadata">

**Author:** ![amca](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/amca/32/11290_2.png) [@amca](https://forum.choiceofgames.com/u/amca)\
**Post date:** [May 5, 2016, 12:36am UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/15 "2016-05-05T00:36:27Z")

</div>

Thanks for double checking for me. I hadnt gotten around to double checking it myself yet.

I’ve been going with programmatically generating the array initialisation code in Choicescript and then copy+pasting that code from the resulting web page into the text editor.

---

<div class="post-metadata">

**Author:** ![stainedofmind](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/stainedofmind/32/459_2.png) [@stainedofmind](https://forum.choiceofgames.com/u/stainedofmind)\
**Post date:** [May 5, 2016, 4:44pm UTC](https://forum.choiceofgames.com/t/initialising-arrays-and-programming-language-implementation/16975/16 "2016-05-05T16:44:03Z")

</div>

Honestly, as a programmer myself, I’ve been seriously considering writing a converting program to turn more program like code into CS coding. At the very least, I’ll probably write up a few helper programs to do things like initializing arrays, making copies of variables for checkpoints, repetitive if/elseif/else codes, etc…
