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!

Redim unnamed nested array

Status
Not open for further replies.

IT53

IS-IT--Management
Apr 18, 2005
2
0
0
US
Hi,
I am trying to Redim a child array nested within a parent array. The child array remains nameless. Here is the meat of the code that generates the following error:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement


arr2d is an existing 2d array to be converted into a nested array of arrays (this is to trim up a listing of repeated entries, but to also catalog the repetition). The code below is executed for every 1st dim entry in arr2d. arr2d is not referenced below.

I've trimmed up the code to focus on the one line to keep it concise.
Code:
For i = 0 to UBound(arrParent) - 1
  'Check to see if repetition exists. If so, execute the
  'line below
  ReDim Preserve arrParent(i)(UBound(arrParent(i))+1)
  'add values into newly expanded child array here.
Next

This seems to be a legal way of referencing and redimming the nested array, but it doesn't work. Any help would be great. Thanks.


 
IT53,

Try this instead. It looks awkward.
[tt]
For i = 0 to UBound(arrParent) - 1
'Check to see if repetition exists. If so, execute the
'line below
[blue]z=ubound(eval("[arrParent(" & i & ")]"))
execute "redim preserve" & "[arrParent(" & i & ")]" & "(" & (z+1) & ")"[/blue]
'add values into newly expanded child array here.
[blue]'do it like this
valuetoassign="new value"
execute "[arrParent(" & i & ")]" & "(" & z & "+1) = " & "valuetoassign"[/blue]
Next
[/tt]
Note, apart from eval and execute, the use of square brackets---it is important.

As it is quick to becoming quite unmanageably clumsy, maybe an assignment of the reference to the dynamic array to the component is better. Whenever a redim is necessary, redim on the dynamic array which reference is so assigned to the component.

regards - tsuji
 
You may also consider playing with Scripting.Dictionary object(s).

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for the posts. I realize I should become familiar with Scripting.Dictionary objects.But in the mean time, I would still like to get to the bottom of this referencing.

Tsuji,
The following line you suggested returns the error:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'UBound'

Code:
    z=ubound(eval("[arrParent(" & i & ")]"))

So I decided to try to research what was happening here, and have confirmed my earlier suspicions of a total lack of decent VBscript refernence on the net.
What is being returned to UBound() after eval processes the bracketed array reference? How/What/Why are the brackets working? Thanks for any help or direction to where I could find an explanation.


 
IT53,

The line itself should be okay if and only if arrParent(i) component is indeed an array. You have arrParent an array with components may or may not be array itself, so when you loop i, for certain i it might error out if it is a simple type string/number.

So within the i loop, verify it vartype.
[tt]
For i = 0 to UBound(arrParent) - 1
if vartype(eval("[arrParent(" & i & ")]"))=8204 then
'do what arrParent(i) being array can do
else
'do not do what should need arrParent(i) be an array
end
Next
[/tt]
It is really not very appealing.

The dictionary object approach is to assign the component of the array to a dictionary. Then the dictionary will take care of the dynamic character.

- tsuji
 
You don't show the original definition of arrParent.

The first declaration should either be a ReDim arrParent(n) or Dim arrParent(). If you use Dim with an actual number, the size of the array is fixed and cannot be changed using ReDim.

e.g.

Dim arrParent()
ReDim arrParent(10)
ReDim arrParent(20)

is ok, but

Dim arrParent(10)
ReDim arrParent(20)

isn't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top