IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesASP Classic  Print This     

Associative Array

A set of unique keys linked to a set of values. Each unique key is associated with a value. Think of it as a two column table. MyArray['CA'] = 'California' MyArray['AR'] = 'Arizona'

Languages Focus

Associative arrays are also known as a dictionary or a hash table in other languages.

ASP Classic:   Scripting.Dictionary

Use the scriptiing dictionary object which is available on later versions of ASP Classic (all still commonly in use).

Note: Both Access VBA and VB Classic use a collection for this but collections are not supported in ASP Classic.

Syntax Example:
Dim StateList
 
set StateList = Server.CreateObject("Scripting.Dictionary")
StateList.Add "CA", "California"
StateList.Add "NV", "Nevada"
 
Response.Write "I live in " & StateList.Item("CA")




Cross Reference Examples:

Access VBA:   Collection

In addition to Add and Item, collections also offer Count and Remove. Notice that Add uses the format of Value, Key (which is backwards from many other languages).

Syntax Example:
Dim States As New Collection
   
States.Add "California", "CA"
States.Add "Nevada", "NV"
    
MsgBox (States.Item("CA"))
C#:   Dictionary
Syntax Example:
//using System.Collections.Generic;
 
Dictionary <String, String> airports = new Dictionary <String, String>();
airports.Add("LAX", "Los Angeles"); 
airports.Add("SFO", "San Francisco");
airports.Add("SAN", "San Diego");
MessageBox.Show(airports["LAX"]);
C++:   map
Syntax Example:
map<string, int> mymap;
mymap.insert(AValuePair);
Corel Paradox:   DynArray

In ObjectPAL associative arrays are known as dynamic arrays.

Syntax Example:
var
myDynArray   DynArray[] String
endVar

 
myDynArray["Last_Name"] = "Spitz"
myDynArray["First_Name"] = "Randy"
myDynArray.view()
Delphi:   TStringList Assoc Array

Object Pascal doesn't have a native associative array, but you can use a TStringList the same way. (Alternatively, search the Internet for TStringHash and THashedStringList classes for implementations of a true associative array).

Syntax Example:
var
  StateList : TStringList;
begin
StateList := TStringList.Create; 
  StateList.CommaText := 'CA=California, FL=Florida';
  ShowMessage('FL is ' + StateList.Values['FL']);
end;
Java:   HashMap()

An associative array links a set of keys to a set of values. In Java, associative arrays are implemented as Maps.

This will print "Arizona."

Syntax Example:
import java.util.*;

public class Maps
{
    public static void main(String[] args)
    {
        Map states = new HashMap();
       
        states.put("CA", "California");
        states.put("FL", "Florida");
        states.put("AZ", "Arizona");

        System.out.println(states.get("AZ"));
    }
}
JavaScript:   Array(), use [
Syntax Example:
var MyStateList= new Array()
MyStateList["CA"]="California";
MyStateList["OR"]="Oregon";
Alert("OR is " + MyStateList["OR"])
Perl:   % Array Preface
Syntax Example:
my %weekdays = (
'Sun' => 'Sunday',
'Mon' => 'Monday',
'Tue' => 'Tuesday',
'Wed' => 'Wednesday',
'Thu' => 'Thursday',
'Fri' => 'Friday',
'Sat' => 'Saturday',
);
my $day_of_the_week = $weekdays{'Mon'};
PHP: 

Declare associative array with initial known values. You can also add to associative array. (You can just assign values without ever declaring it too!)

Syntax Example:
$prices = array( 'Tires'=>100, 'Spark Plugs'=>4 );
$prices['Oil'] = 10;
 
echo "Tires=" . $prices['Tires'] . "<br>";
echo "Oil=" . $prices['Oil'] . "<br>";
VB Classic:   Collection

In addition to Add and Item, collections also offer Count and Remove. Notice that Add uses the format of Value, Key (which is backwards from many other languages).

Syntax Example:
Dim States As New Collection
   
States.Add "California", "CA"
States.Add "Nevada", "NV"
    
MsgBox (States.Item("CA"))
VB.Net:   Dictionary

An associative array links a set of keys to a set of values. In Visual Basic, associative arrays are implemented as Dictionaries.

This code produces a message box saying "Nevada."

Syntax Example:  
//Imports System.Collections.Generic
Dim States As New Dictionary(Of String, String)

States.Add("CA", "California")
States.Add("NV", "Nevada")
 
MsgBox(States("NV"))




Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]