Using Multidimensional Arrays with VBScript

in utopian-io •  7 years ago  (edited)

What Will I Learn?

How to Work with More than One Dimension in VBScript Programming using Emacs

Requirements

GNU Emacs
Github: https://github.com/emacs-mirror/emacs
Download link: https://www.gnu.org/software/emacs/download.html

Difficulty

Intermediate

Tutorial Contents

  • How to Create A Simple VBScript Array
  • A Two Dimensional Array
  • A Three Dimensional Array
  • Nth Dimensional Arrays

VBScript multidimensional arrays enable a programmer to store data in complex matrices. By using multidimensional array a programmer can create a matrix of data rather than a list.

However, before diving into the rather more complex world of VBScript multidimensional arrays it is probably best to consider a simple array first.

How to Create A Simple VBScript Array

Simple VBScript array:

Option Explicit
Dim Names: Names = Array ("Fred", "John", "Joan")
Dim x: For x = 0 to (ubound(Names))
wscript.echo Names(x)
Next


A Two Dimensional Array

If a simple VBScript array is analogous to a list then a two dimensional array is analogous to a grid of data (and is often referred to as a rectangular array). Here a 5 by 8 grid is defined:

Dim WorkingWeek (4, 7)
WorkingWeek (0,0) = "Start working on Monday"
WorkingWeek (0,3) = "Remember to have lunch"
WorkingWeek (4,7) = "Stop working on Friday"


image.png

It's always worth remembering that an array starts with index number 0 and not 1. Bearing that in mind the contents of the array can now be accessed:

Dim x, y
For y = 0 to ubound (WorkingWeek)
For x = 0 to ubound (WorkingWeek, 2)
If WorkingWeek (y,x) <> "" Then
wscript.echo WorkingWeek (y,x)
End If
Next
Next



It's worth noting the fact that the dimension number is used here with ubound in order to obtain the number of elements in the additional dimension.

A Three Dimensional Array

If a one dimensional array is analogous to a list, and a two dimensional array is analogous to a grid, then a three dimensional array is analogous to a cube (for example a Rubik's cube). The array elements are assigned in the same ways as with two dimensions (except, of course that there is an extra dimension):

Dim cube (3, 3, 3)
cube (0,0,0) = "Top Left Front"
cube (1,1,1) = "Center"
cube (2,2,2) = "Bottom Right Back"



And the 3 dimensional array can be used in a similar manner to 2 dimensions:

Dim x, y, z
For y = 0 to ubound (cube)
For x = 0 to ubound (cube, 2)
For z = 0 to ubound (cube, 3)
If cube (y,x,z) <> "" Then
wscript.echo y, x, z, cube (y,x,z)
End If
Next
Next
Next


Nth Dimensional Arrays

It is possible to have a maximum of 32 dimensions in a VBScript multidimensional array, but more than that will cause VBScript to run out of memory. However, since that's more than enough dimensions for a Quantum physicist then it's probably enough for the average VBScript programmer.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.

  • This post is too short and tutorial not completed.

You can contact us on Discord.
[utopian-moderator]