I don’t know about you, but arrays have always been a weird topic for me. I’ve never really found a good use for them in my applications, but I know they do have their uses. So today, we’re going to start off with a regular array expression, explain what it is and how to go through it to show our values.
An array is a collection of information. It uses an index number and can proceed from there. By using an array, you can easily have one variable name that can have many different types of information based upon it’s index number. Consider this example that assigns a different name to each index number of our names array.
names(0) = “Mike”
names(1) = “Scott”
names(2) = “Brenda”
names(3) = “Travis”
names(4) = “Rachel”
names(5) = “Kevin”
The first thing we did was declare a variable name that would hold our array. Then we also declared that this array would have six positions. Finally, we gave each position in the array a different value. To show the value of a certain position, all we have to do is something like this.
The output would be Rachel.
We can also display all the contents of our array by using a for each…next loop. Consider this example.
For Each name in names
response.write(name) & “
What we did here was run a loop that would run through our names array and pull out each name and response.write it to our screen. When using a loop like this, you just need to make up a new variable name to use as your counter when the loop is running. In this case we used the variable “name” to be our counter. You can use arrays to iterate through the values from a form. Instead of doing this and constantly having to come up with different variable names:
variable_name = trim(request.form(“fieldname”))
You could do this:
variable_name(0) = trim(request.form(“fieldname”))
variable_name(1) = trim(request.form(“fieldname”))
I know there are other uses for arrays, but as of right now, I’ve only come across them in my e-commerce situations where I need to store multiple pieces of information about one product into a variable. If you can think of any other uses for an array, please let us know so we can include that in our examples and build some exercises from them. Next time we’ll be dealing with 2 dimensional arrays. The best way to think of a 2 dimensional array is to think of an Excel spreadsheet. You have your fields going across the top, which would be your first dimension in the array. Then you have a row for each amount of information added. Each row would be contained in your 2nd dimension. But we’ll discuss that more later. Have fun with this one and be sure to post your questions and comments on our messageboard. See ya next time!
Times Viewed: 15