Arrays – again!

Home/Moving on.../Arrays – again!
Arrays – again! 2016-12-28T08:39:42+00:00

Associative Arrays

Earlier we looked at arrays that you could create and use.  This chapter looks at how arrays are used by Small Basic – for listing files and folders on your filling system, and extends your knowledge into the field of Associative arrays.  More on this later.  This first section looks at the filing system…

Files and folder

ListFilesSmall Basic uses arrays to provide access to your filing system.  I.e. to list files and folders on the storage media on your PC.  Thus if you want to know what files you have saved in a particular folder, or you want to know what folders exist then Small Basic can tell you but only in the form of an array.  Look at the example on the left.

Program.Directory returns the folder where the program is running.  This is why I always recommend saving before you run a program – you know where you saved it and can check the information that Small Basic is returning to you!

File.GetFiles( path ) gets all the files that are in the folder specified by ‘path’, i.e. the folder where you saved the program.  Even if it is just one file it will retuned in an array.

Array.GetItemCount( array ) tells you how many file there are so that the main body of the program can simply loop through the array, displaying each one.

If you have run this program, you will see that Small Basic returns the full filename including the path.  It should be possible to remove the path bit using one of the Text object methods.

As well as being able to return an array of files, you can also ask Small Basic to return a list of sub-folders in a specific folder. However, if your folder doesn’t contain any sub-folders this method will return an empty simple variable as the result (not ‘FAILED” as in the intellisence panel).  Any program written to do this must accommodate this possibility.

Program demonstration Associative Arrays

Associative Arrays

As an alternative to arrays that use a number as the index to an element in an array, Small Basic can also use words as the index.  As an example, you earlier created arrays for the months of the year using indices 1 to 12.  It is also possible to create elements in that array as Month[ “January” ], Month[ “February” ], etc.  Doing this enables you to store a property of the month of January in an element indexed by the word  “January”.  This makes Small Basic arrays very powerful.  A number of high level programming languages allow this.

Look at the program on the right – click on it to expand it.   (It’s an image so you cannot copy it, but I have provided a link to download it as a zip file if this is possible on your PC).  Download this program as a zip file.  I do recommend entering this program so that you can see how it works, modify it and get an understanding of associative arrays.  They are probably not explicitly required for GCSE in the UK but that does not mean you cannot use them.

Objects

Box5R

Arrays
These are the Methods / Functions available with the Array object.  You have seen these before but pay particular attention to those that work with associative arrays.

Box5R

Files
These are the Functions available with the File Object.

Searching Arrays

One of the most important tasks that you will need to be able to do with arrays is to search them for specific values.  Small Basic gives an easy way to find out if a value is in an array but it doesn’t tell us where it is.  Small Basic really needs a function ‘Array.GetIndexOf()’ but it doesn’t have one so we need to be able to program it ourselves.  It will be useful whenever we work with arrays and/or files.

To do it we need to analyse how we, as humans (assuming you are), search through a list and decide if we have found an entry (or not).  We are  going to assume that the list is not in any particular order as this does affect how we work.

With an un-ordered list, we have to start at the beginning (or end) of the list and work through each entry until we either find the required element or we get to the other end of the list.

In an unordered list there will only be these two outcomes, We either find the element we are looking for or we arrive at the end of the list.

This implies that for our program, we need to continually check for these two outcomes.  Below I have set this out in pseudo code and as a flow chart.

ArraySearchNotice that the While… …EndWhile loop checks to see if the entry was found AND if we are NOT at the end of the list.  Inside this loop a simple If… …Then… …Else decision just checks to see if the entry has been found.

When the code finishes, all we need to do is check to see if the variable ‘Found’ is True.  If it is then the data was found and the value held in ‘Index’ is the index to the required data in the array.  We can then do whatever we need with the Array[ Index ] data.  Of course this algorithm has assumed that the array uses numeric indexes but it can be modified to work with word indexes if required.

End of Topic Tasks