Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filling an array ... problem

Status
Not open for further replies.

QTip

Programmer
Mar 7, 2001
66
0
0
BE
Hello,

I want to put some data into an array. But the problem is that this data is variable, so I can't say how big the array will be. So I can't declare the array with a constant size. How can I make this variable? When I do like here below, it doesn't work. Can somebody help me? thnx!!!

strCount = 23 // just as example, this is variable.
Dim strArray(strCount), strTeller
For strTeller = 0 to strCount
strArray(strTeller) = .........some code ......
Next
.....
 
Dim strArray(1) 'Just a starting point

Dim lngCount 'It's a number
Dim lngTeller 'It's a counter so it's a number

lngCount=23

for lngTeller=0 to lngCount
redim preserve strArray(lngTeller + 1)
strArray(lngTeller) = "This is number " + lngTeller
next

It's better however to do it in chunks. For example if you think you'll need 20 items, create an array with room for 30 if it gets full, assign another 20. The loop I wrote (above) would get very slow if lngCount were a large number, as the script engine has to create a new array of the size requested, populate it from the old array then delete the old array.


 
1. Create a comma separated string from your input data.
2. Use the array function to create the array.

Assuming CVS string has been created
Dim A 'will contain array
A = Array(strCvs) 'create array using comma separated string
 
Hi HectorShuckle, your explanation seems very reasonable but when I tried it, I got next error:

Error Type:
Microsoft VBScript runtime (0x800A000A)
This array is fixed or temporarily locked

He gives the error on this line:
redim preserve strArray(lngTeller + 1)

 
this might help: the array should normally not be bigger than 100. It is possible, but very unlikely.
 
Be sure you've DIMmed with an actual number, rather than a variable.

Dim arrX(theNumber) 'doesn't work
Dim arrX(77) 'should work
 
no, I'm sorry ... I dim like this Dim arrX(100) and he still gives the same error.

Thnx anyway!
 
taking your original post....

strCount = 23 // just as example, this is variable.
Dim strArray(strCount), strTeller
For strTeller = 0 to strCount
strArray(strTeller) = .........some code ......
Next

first you should dim the array as dynamic and reDim then
eg:
Dim strArray()
reDim strArray(0)
this takes out locked errors

now you acn work with it
say for a example you always want to increase the elements by one you could use the ubound() value
Dim counter : counter = UBound(strArray)
counter holds 0 now
given another factor, say a recordset to loop through this can give you a great storage ability
eg:
Do WHILE NOT RS.EOF
reDim preserve strArray(counter)
strArray(counter) = RS(0)
counter = counter + 1
RS.MoveNext
Loop

so for simplicity your for each would equate to
Dim strCount : strCount = 23
Dim strArray()
reDim strArray(0)

Dim strArray(strCount), strTeller
For strTeller = 0 to strCount
reDim strArray(strTeller)
strArray(strTeller) = "foo"
Next

_____________________________________________________________________
[sub]Where have all my friends gone to????[/sub]
onpnt2.gif

 
take that line I left in there out
Dim strCount : strCount = 23
Dim strArray()
reDim strArray(0)

Dim strArray(strCount), strTeller
For strTeller = 0 to strCount
reDim strArray(strTeller)
strArray(strTeller) = "foo"
Next

_____________________________________________________________________
[sub]Where have all my friends gone to????[/sub]
onpnt2.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top