Arrays
So far we've looked at the basic variables types such as strings and integers, as well as a variety of functions you can use to manipulate these data types. Beyond the basic data types are arrays and objects, which take a little bit more work in order for you to take advantage of them properly. More importantly, arrays take a solid understanding of functions before you try to use them, which is why they are separated here! As mentioned, objects are covered exclusively in their own chapter - this chapter is dedicated to the topic of arrays.
In order to model our surroundings in a programming environment accurately, it is important to recognise that some types of data naturally group together - colours, for example, naturally clump together into one group. Rather than having hundreds of separate variables - one for each colour - the reality is that it makes more sense to have one variable that holds a list, or array of colours.
Topics covered in this chapter are:
-
Reading arrays
-
Manipulating arrays
-
Multidimensional arrays (arrays of arrays)
-
Saving arrays
Chapter contents
- 5.1. First steps
- 5.2. Associative arrays
- 5.3. The two ways of iterating through arrays
- 5.4. The array operator
- 5.5. Returning arrays from functions
- 5.6. Array-specific functions
- 5.6.1. Chopping and changing arrays
- 5.6.2. Stripping out duplicate values
- 5.6.3. Filtering your array through a function
- 5.6.4. Converting an array to individual variables
- 5.6.5. Checking whether an element exists
- 5.6.6. Using an array as a double-ended queue
- 5.6.7. Swapping keys and values
- 5.6.8. Sorting arrays
- 5.6.9. Grabbing keys and values
- 5.6.10. Randomising your array
- 5.6.11. Creating an array of numbers
- 5.7. Multidimensional arrays
- 5.8. The array cursor
- 5.9. Holes in arrays
- 5.10. Arrays in strings
- 5.11. Saving arrays
- 5.12. Summary
- 5.13. Exercises
- 5.14. Further reading
- 5.15. Next chapter


Copyright 2010 Future Publishing Limited (company
registered number 2008885), a company registered
in England and Wales whose registered office is at
Beauford Court, 30 Monmouth Street, Bath, BA1 2BW, UK