ASP: Arrays

That’s assumed that you are aware of fundamental features of arrays, so let’s consider how they are handled in ASP, in VBScript.

The VBScript arrays are 0 based, meaning that the array element indexing starts always from 0. The 0 index represents the first position in the array, the 1 index represents the second position in the array, and so forth.

There are two types of VBScript arrays – static and dynamic. Static arrays remain with fixed size throughout their life span. To use static VBScript arrays you need to know upfront the maximum number of elements this array will contain. If you need more flexible VBScript arrays with variable index size, then you can use dynamic VBScript arrays. VBScript dynamic arrays index size can be increased/decreased during their life span.

Static Arrays

Let’s create an array called ‘arrCars’ that will hold the names of 5 cars:

 
<%@ LANGUAGE="VBSCRIPT" %>
<%
'Use the Dim statement along with the array name
'to create a static VBScript array
'The number in parentheses defines the array’s upper bound
 
Dim arrCars(4)
arrCars(0)="BMW"
arrCars(1)="Mercedes"
arrCars(2)="Audi"
arrCars(3)="Bentley"
arrCars(4)="Mini"

'create a loop moving through the array
'and print out the values
For i=0 to 4
response.write arrCars(i) & "<br>"
Next     'move on to the next value of i
%>

Here is another way to define the array in VBScript:

 
<%
'we use the VBScript Array function along with a Dim statement
'to create and populate our array

Dim arrCars
arrCars = Array("BMW","Mercedes","Audi","Bentley","Mini") 'each element must be separated by a comma

'again we could loop through the array and print out the values

For i=0 to 4
response.write arrCars(i) & "<br>"
Next
%>

Dynamic Arrays

Dynamic arrays come in handy when you aren’t sure how many items your array will hold. To create a dynamic array you should use the Dim statement along with the array’s name, without specifying upper bound:

<%
Dim arrCars
arrCars = Array() 
%>

In order to use this array you need to use the ReDim statement to define the array’s upper bound:

<%
Dim arrCars
arrCars = Array() 
Redim arrCars(27)
%>

If in future you need to resize this array you should use the Redim statement again. Be very careful with the ReDim statement. When you use the ReDim statement you lose all elements of the array. Using the keyword PRESERVE in conjunction with the ReDim statement will keep the array we already have and increase the size:

<%
Dim arrCars
arrCars = Array() 
Redim arrCars(27)
Redim PRESERVE arrCars(52)
%>
admin

admin

Leave a Reply

Your email address will not be published.

JavaScript: Arrays

Arrays are a fundamental part of most programming languages and scripts. Arrays are simply an ordered stack of data items. Each element of the array can store its own data, just like a variable, thus you can say arrays are collections of variables. Items can be added and removed from the array at any time, also their value can be changed easily. One other feature of the arrays, which is specific to JavaScript is that the elements in the array can be of different types. For example in an array you can have both a string and an integer.

Using arrays, you can store multiple values under a single name. Instead of using a separate variable for each item, you can use one array to hold all of them.

Creating Arrays

There are a few different ways to create an array. The old way of creating arrays to involve the Array() constructor. JavaScript arrays are dynamic, so you can declare an array and do not pass any arguments with the Array() constructor. In this case you will create an empty array with no elements.

 
<script type="text/javascript">

//We initialize the array using the array() constructor.
var first_array = new Array();

first_array[0] = "This is an element";
first_array[1] = 5;
first_array[2] = "JavaScript - Tutorial";
first_array[3] = 16;
first_array[4] = 7;

var counter=0;

//Let's print out the elements of the array.
for (counter=0; counter<first_array.length; counter++)
   document.write(first_array[counter] + "<br>");

</script>

To declare an array with the specified number of elements you should pass a single integer as an argument. If you pass more than one argument then the number of elements will be equal to the number of data values specified. If you specify numbers or true/false values inside the array then the type of variables will be numeric or Boolean instead of string. Array’s elements are accessed using their index, which starts from 0.

 
<script type="text/javascript">

//We declare the first array and pass a single integer as an argument..
var cars = new Array(5);

cars[0] = "Audi";
cars[1] = "Bentley";
cars[2] = "Mercedes";
cars[3] = "Mini";
cars[4] = "BMW";

//Now we declare the second array and pass 8 arguments.
//This technique does not work in JavaScript 1.2.

var flowers = new Array("Rose", 2.45, "Daisy", 1.57, "Orchild", 0.75, "Tulip", 1.15);

var counter=0;
document.write("<h1>Elements of the first array:</h1>");

for (counter=0; counter<cars.length; counter++)
   document.write(cars[counter] + "<br>");

counter=0;
document.write("<h1>Elements of the second array:</h1>");

for (counter=0; counter<flowers.length; counter++) {

   if (counter % 2 == 0) {
      document.write(flowers[counter] + " costs ");
   }
   else {
      document.write(flowers[counter] + "<br>");
   }

}

</script>

Back to top

Associative Arrays

Associative arrays are arrays that allow you to call the array element you need using a string rather than a number, which is often easier to remember. The downside is that these aren’t as useful in a loop because they do not use numbers as the index value. Have a look at the following example:

 
<script type="text/javascript">

var first_array = new Array();
first_array["key1"] = "the first element";
first_array["key2"] = "the second element";

var second_array  = new Array();
second_array["key3"] = "this is the first element of the second array";
second_array["key4"] = "this is the second element of the second array";

document.write(first_array["key1"] + "<br>");   //prints "the first element."
document.write(second_array["key3"] + "<br>");  //prints "the first element of the second array"
document.write(first_array["key2"] + "<br>");   //prints "the second element"
document.write(second_array["key4"] + "<br>");  //prints "this is the second element of the second array"

</script>

Because the indices in this associative array are not numbers, we cannot use a simple counter in a for loop to work with the array. The way to iterate over the items in an associate array is to use the for (value in array) construct, allowing you to access each item’s value via array[value]. Have a look at the example:

 
<script type="text/javascript">

//We initialize the array using the Array() constructor. 
//Note that for readability one can spread the argument over several lines.

var flower_shop = new Array ();

flower_shop["rose"] = "5.00";
flower_shop["daisy"] = "4.00";
flower_shop["orchid"] = "2.00";

//let's print out the headers to our table
document.write("<table border=\"1\" cellpadding=\"5\">");
document.write("<tr><th>Flower</th><th>Price</th></tr>");

//Now we start the for loop using the variable flower to hold our key.
for ( var flower in flower_shop) //print the values into a table cell for each iteration
  document.write( "<tr><td>" + flower + "</td><td>" + flower_shop[flower] + "</td></tr>");

//finally close the table
document.write ("</table>");

</script>

Back to top

Multidimensional Arrays

In the preceding examples you’ve learned how to use arrays. But what if you want to give more information on each flower? You now have the cost, but what if you wanted to add the number of flowers you get for that price, and the colour of the flower? One of the ways to do it is using multidimensional arrays.

A multidimensional array is an array that contains at least one other array as the value of one of the indexes. Example below shows how to use multidimensional array:

 
<script type="text/javascript">

//Initialize the array using the Array() constructor.
var flower_shop = new Array();

flower_shop['rose'] = new Array( "5.00", "7 items", "red" );
flower_shop['daisy'] = new Array( "4.00", "3 items", "blue" );
flower_shop['orchild'] = new Array( "2.00", "1 item", "white" );

//print "rose costs 5.00, and you get 2 items."
document.write( "rose costs " + flower_shop['rose'][0] + ", and you get " + flower_shop['rose'][1] + ".<br>");
//print "daisy costs 4.00, and you get 3 items." 
document.write( "daisy costs " + flower_shop['daisy'][0] + ", and you get " + flower_shop['daisy'][1] + ".<br>");
//print "orchild costs 2.00, and you get 1 item. 
document.write( "orchild costs " + flower_shop['orchild'][0] + ", and you get " + flower_shop['orchild'][1] + ".<br>");

</script>

Back to top

admin

admin

Leave a Reply

Your email address will not be published.

PHP: Arrays

Arrays can be used in many ways to store and organize data quickly and efficiently. It is one of the more useful data types available to any programming language.

Arrays can most easily be described as an ordered list of elements. You can access the individual elements by referring to their index position within the array. The position is either specified numerically or by name. An array with a numeric index is commonly called an indexed array while one that has named positions is called an associative array. In PHP, all arrays are associative, but you can still use a numeric index to access them.

An Example of an indexed Array:

<?php 
$seven = 7;
$arrayname = array( "this is an element", 5, $seven );

echo $arrayname[0];   //prints: this is an element
echo $arrayname[1];   //prints: 5
echo $arrayname[2];   //prints: 7
?>

As you can see, elements in an array can be any type of data (string, integer, double) and can also be other variables. An individual array element can be of any type, including another array.If you want to find out if a variable contains an array you can use the is_array() function. Notice that Indexed arrays start at position zero, not at position one, so your first array element has an index of 0, and the highest position in the array is one less than the number of elements in the array.

Associative Arrays

Associative arrays are arrays that use named keys that you assign to them. Have a look at the following example:

<?php 
$first_array = array("key1" => "the first element", "key2" => "the second element");

$second_array = array(
    "key3" => "this is the first element of the second array",
    "key4" => "this is the second element of the second array",
);
echo $first_array['key1'];    //prints "the first element."
echo $second_array['key3'];   //prints "the first element of the second array"
echo $first_array['key2'];    //prints "the second element"
echo $second_array['key4'];   //prints "this is the second element of the second array"
?>

Right, now you know how to define an associative array, but you probably don’t see yet how useful are they. Well think of this, say you have a flower-shop. You have 3 different flowers, and each flower has a different price. Let’s make this example in php.

<?php 
//We initialize the array using the array() function. 
//Note that for readability one can spread the argument over several lines.

$flower_shop = array (
     "rose" => "5.00",
     "daisy" => "4.00",
     "orchid" => "2.00"
);

echo "rose costs $flower_shop['rose'], daisy costs $flower_shop['daisy'], and orchild costs $flower_shop['orchild'].";
?>

Because the indices in this associative array are not numbers, we cannot use a simple counter in a for loop to work with the array. We can use the foreach loop. In the following example we use the foreach loop to iterate through our flowers_shop array, and read them into a table. Note carefully the syntax.

<?php
//We initialize the array using the array() function. 
//Note that for readability one can spread the argument over several lines.

$flower_shop = array (
     "rose" => "5.00",
     "daisy" => "4.00",
     "orchid" => "2.00",
);
//let's print out the headers to our table
echo "<table border='1' cellpadding='5'>";
echo"<tr><th>Flower</th><th>Price</th></tr>";
//Now we start the foreach loop using the variable $flower to hold our key
//and $price to hold our cost.

foreach($flower_shop as $Flower=>$Price)
{
  echo "<tr><td>$Flower </td><td>$Price</td></tr> "; //print the values into a table cell for each iteration
}
//finally close the table
echo "</table>";
?> 

Multidimensional Arrays

In preceding example you’ve learned how to use arrays. But what if you want to give more information on each flower? You now have the cost, but what if you wanted to add the number of flowers you get for that price, and the colour of the flower? One of the ways to do it is using multidimensional arrays.

A multidimensional array is an array that contains at least one other array as the value of one of the indexes. Example below shows how to use multidimensional array:

<?php 
//Initialize the array using the array() function.
$flower_shop = array(
"rose" => array( "5.00", "7 items", "red" ),
"daisy" => array( "4.00", "3 items", "blue" ),
"orchid" => array( "2.00", "1 item", "white" ),
);

//print "rose costs 5.00, and you get 7 items."
echo "rose costs ".$flower_shop['rose'][0].", and you get ".$flower_shop['rose'][1].".";
//print "daisy costs 4.00, and you get 3 items."
echo "daisy costs ".$flower_shop['daisy'][0].", and you get ".$flower_shop['daisy'][1].".";
//print "orchild costs 2.00, and you get 1 item.
echo "orchid costs ".$flower_shop['orchid'][0].", and you get ".$flower_shop['orchild'][1].".";
?>

admin

admin

Leave a Reply

Your email address will not be published.