ASP Junction Home Of EZCodes
3 visitors online
149823 total visitors

Sunday, 1 August, 2010
Home Downloads Articles Programming Portfolio Support Privacy  




Newsletter


The HTMLJunction Store

Top 5 Downloads
EZNewsletter: 11326
EZGallery: 6518
EZPoll: 6375
EZUpload: 5818
EZGuestbook: 4486
Download Stats
EZHomepagePro: 3632
EZHomepageBasic: 1573
EZNewsletter: 11326
EZOnlineEditor: 4032
EZMedia: 3776
EZGuestbook: 4486
EZGallery: 6518
EZPoll: 6375
EZScheduler: 2432
EZUpload: 5818
Total: 50684
HTMLJunction News
ASP Article - Dynamic Date Dropdown Menu.

ASP Article - Import Text File to an Access Database.

EZScheduler now available for download!

ASP Junction has just been launched!

HTMLJunction has a new store!


Advertisements






HTMLJunction is against any kind of spyware!
ASP Article
Rated out of 5 stars - 7 Total Votes


Working with Multi Dimensional Arrays.



In this article we will look at Multi Dimensional Arrays, Focusing on the Nontraditional Arrays.

I was trying to load a Database table with a list of Countries and their abbreviations. I was first looking at the traditional way of doing it using arrays:

Dim arrCountries(1,246)

arrCountries(0,0) = "US"
arrCountries(0,1) = "United States"
arrCountries(1,0) = "CA"
arrCountries(1,1) = "Canada"
.
.
.
      

But with 247 Countries and their abbreviations I was looking at declaring almost 500 variables in the array...something I wasn't looking forward to and I thought there had to be a better way.

I started googling the problem and everything I found was explained like the example above. I had at first thought I could do it the way it is done in the Java and C languages:

Dim arrCountries(1,246)

arrCountries(("US,United States"),("CA,Canada"),...,("ZM,Zambia"),("ZR,Zaire"))
      

That didn't work so I tried some other different ways

Dim arrCountries(1,246)

arrCountries("US","United States","CA","Canada",...,"ZM","Zambia","ZR","Zaire")

arrCountries(("US","United States"),("CA","Canada"),...,("ZM","Zambia"),("ZR","Zaire"))
      

I even tried:

 
Dim arrCountries

arrCountries = Array(("US,United States"),("CA,Canada"),...,("ZM,Zambia"),("ZR,Zaire"))

arrCountries = Array("US","United States","CA","Canada",...,"ZM","Zambia","ZR","Zaire")

arrCountries = Array(("US","United States"),("CA","Canada"),...,("ZM","Zambia"),("ZR","Zaire"))
      

All this did was throw a bunch of errors and after much trial and error what I found was that ASP cannot recognize a multi dimensional array in this manner, it can only see a multi dimensional array like it is shown in the first example.

When I tried to iterate through the Arrays in the tradinional manner:

 
For ix = 0 to UBound(arrCountries,2)
  Response.Write "Abbr: "&arrCountries(0,ix)
  Response.Write "Country: "&arrCountries(1,ix)
Next
      

Only the first example worked, all the others kept giving me the 'Subscript out of range' or 'Missing ")"' or 'Type mismatch [string]' errors

So I had to look at it in 1 dimension...that is a 1 dimensional array of strings.

What I found is that if I used this example

Dim arrCountries

arrCountries = Array("US,United States","CA,Canada",...,"ZM,Zambia","ZR,Zaire")
      

and treated each element as a string I could work with it using the "split" function in this manner

 
For ix = 0 to UBound(arrCountries)
  x = split(arrCountries(ix),",")
  Response.Write "Abbr: "&x(0)
  Response.Write "Country: "&x(1)
Next
      

What is strange here is that the "split" function is an array function Arrays

And if I check it to see if its an array it comes up false:

 
For ix = 0 to UBound(arrCountries)
  Response.Write IsArray(arrCountries(ix))
Next
      

Summary:

If you have a large amount of data to work with and it is multi dimensional the best (possibly the only) way to do it is:

Dim arrCountries

arrCountries = Array("US,United States","CA,Canada",...,"ZM,Zambia","ZR,Zaire")
       
For ix = 0 to UBound(arrCountries)
  x = split(arrCountries(ix),",")
  Response.Write "Abbr: "&x(0)
  Response.Write "Country: "&x(1)
Next
      

BTW...Here is the script I used to get the countries and their abbreviations, just add your connection object and swap out the "Response.Write" with your querystring and you're good to go.

Well that's the nitty gritty of it. I hope you find this article useful!

Rate this Article
Very Good Good OK Bad Very Bad
Advertisements








We use Google Sitemaps to inform Google's crawler about all your pages and to help people discover more of your web pages.

Advertisements






Resources

Gunners Gallery
Wesley Ford
seo-advantage.com

copyright © 2010 ASP Junction
An HTMLJunction website