TWX Arrays, Part 1

Array Basics

I think of arrays like pill boxes.  You know, the round kind that has 7 sections, 1 for each day of the week.  To stock the pill box, you turn the window to section 1 and add that day’s pills.  Then you turn it to 2 and add that day’s pills.  And so on.  The little window only opens onto one section at a time.  So when it is time to retrieve the pills for day 5, you spin the window until it is over 5, then you dump the contents into your hand.  Congratulate yourself, you’ve just retrieved index 5 from an array.

So, in TWX scripting, that would look something like this:


setVar $pillbox[1] “Day 1 Pills”

setVar $pillbox[2] “Day 2 Pills”

setVar $pillbox[3] “Day 3 Pills”

setVar $pillbox[4] “Day 4 Pills”

setVar $pillbox[5] “Day 5 Pills”

setVar $pillbox[6] “Day 6 Pills”

setVar $pillbox[7] “Day 7 Pills”

# And now to dump the contents of section 5

echo $pillbox[5]

Of course, you’d really want to use a while loop to populate the array:



setVar $i 1

while ($i <= 7)

     setVar $pillbox[$i] “Day “ & $i & " Pills”

      add $i 1


echo $pillbox[5]

2-Dimensional Array

Ah, but you have bigger problems.  You see, your aunt needs you to lay out all of her pills for her, but you can only visit her once a year.  So here is what you do, you find a big pill box that has 12 sections, one for each month.  Then you hunt down some pillboxes that have 31 sections to them, and you buy 12 of them.   So, to be clear, you have 13 pillboxes: 1 large 12-compartment pillbox, and 12 small 31-compartment pillboxes.  After you make sure that your aunt’s medicine has a shelf-life of at least a year, you start stocking the first little pillbox.  When you have all 31 “day” compartments filled, you grab the large “month” pillbox, open compartment 1, and drop the “day” pillbox inside.  So we’ll call the large pillbox $pillbox.  Compartment 1 (January) will be referenced as $pillbox[1].  And of course, compartment 12 (December) will be referenced as $pillbox[12].  Now, inside of $pillbox[1] you’ve just placed a small pillbox with 31 compartments, one for each day, and these day compartments are referenced as $pillbox[1][1] through $pillbox[1][31].  Keep filling “day” pillboxes and adding them to “month” compartments until all 12 months are done, the last one of course being $pillbox[12][31].  It should now be rather obvious what the reference is for the compartment associated with month 6, day 22 for instance: $pillbox[6][22].

Now, you know that we are lazy, and we’ll streamline the process of filling those pillboxes with a couple of while loops.  Similar to the arrays themselves, we’ll have a main while loop that opens each of the 12 month compartments, and a loop inside of that one (called a nested loop) to fill up the 31 day compartments.



setVar $month 1

while ($month <= 12)

     setVar $day 1

      while ($day <= 31)

           setVar $pillbox[$month][$day] "Month " & $month & ", Day " & $day & " Pills"

            add $day 1


     add $month 1


# Dump the contents for month 6, day 22

echo $pillbox[6][22]