A stack is an ordered list where entries are added (pushed) and retrieved (popped) in a last-in first-out order. The name stack come from the fact that it is like a stack of plates. When we add a plate it goes on the top and when we remove a plate it is the last one added that is removed.
A stack is useful to store things that we want process gradually as we unwind the stack, retrieving things to process. Often the things to add to the stack are not known at the start, but are the result of processing previous stack values. If we knew them all at the start we could just put them in an array.
Only three methods are present, they are:
The stackName is a string, that is something enclosed in double quotes, for example "my_stack". The value may be any Small Basic variable or value.
We want to create an array with all of the jpg image files in a directory and all sub-directories, searching all folders recursively.
First we create an empty array to store the image file paths and the number of files in it (zero to start with). We also set the starting folder to search and add it to a stack we call "folders". In this example the starting folder location is the folder where the Small Basic source file is saved (Program.Directory).
images
=
""
imageCount
0
startFolder
Program
.
Directory
Stack
PushValue
(
"folders"
,
)
Next we want to keep working while we have folders still to check, or the stack still has some folders in it. We use a while loop for this. Inside the while loop we want to process the last folder added, so it will be popped first.
While
GetCount
>
currentFolder
PopValue
' More work to do here
EndWhile
We now want to process this folder; first we get all the sub-folders in the current working folder and push these onto the stack for later checking as the while loop repeats.
folders
File
GetDirectories
For
i
1
To
Array
GetItemCount
[
]
EndFor
Having added the sub-folders to check later as the stack unwinds, we find all the files in the current working folder and add ones ending in ".jpg" to the array list. We check the file by first converting to lower case, in order to include all case variants of jpg JPG etc.
files
GetFiles
fileName
fileNameLower
Text
ConvertToLowerCase
If
EndsWith
".jpg"
Then
+
EndIf
Finally we print out the results, and below is the whole thing.
TextWindow
WriteLine
litdev edited Original. Comment: typos
This is awesome! Thanks for the contribution, LitDev!
I featured this article here: blogs.msdn.com/.../small-basic-stack-basics-featured-article.aspx