PDA

View Full Version : array help


esthera
05-25-2006, 09:06 AM
2 questions:
1. I want to make an array but I don't know in advance how many elements it will have -- how can I do this?
2. Once I have an array -- is there any easy way to sort it alphebetically?

ghell
05-25-2006, 01:23 PM
as far as i know there is no variable length array in ASP. You could write functions to insert into an array at a given index (>= 0) and if its not big enough, copy the contents of the array to a new array which can fit in the new variable. this is how most languages have Vectors etc programmed anyway (they employ other tricks such as size, capacity and growth rate for performance as copying the contents of the array is a big hog)

there is no easy way to sort an array alphabetically you need to write your own sorting algorithm (not hard if you only care about the first letter) to chech the Chr(Mid(str, 1, 1)) or something.

degsy
05-25-2006, 03:57 PM
If you are doing a loop then you can redim the array within the loop.


'Dim imgArr()
set fo=fs.GetFolder(path)
for each x in fo.files
'Print the name of all files in the test folder
Redim Preserve imgArr(i)
imgArr(i) = x.name
i=i+1
'Response.Write x.Name & "<br>"
next
set fo=nothing
set fs=nothing


http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=83

esthera
05-25-2006, 04:04 PM
is doing that bad --
so in this case I would start
dim arrc(0)

and then redim within the loop?

degsy
05-26-2006, 02:46 PM
You can set the array to anysize really because you are going to redim it anyway.

ghell
05-26-2006, 04:32 PM
Nice I wasnt aware of the Preserve keyword for ReDim! Learn something new every day :thumbsup:

yesterday I learned: "users cant distinguish between message boxes and errors. also they never READ errors or do anything they instruct.. so dont bother putting any messageboxes or error messages anywhere just crash out silently and give them an invalid email address to send complaints to :D"