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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrays copying

Status
Not open for further replies.
Hi,

I don't know if you have tried: a() = b()

Or what might work:
For i = 0 to Ubound(a)
b = a(i)
next


Grtz Hans
 
Of course, that's what I have currently: the iteration through each item

For each...
b(i,k) = a(i,k)

But that seems very inefficient. That's why I asked.


"I don't know if you have tried: a() = b()"

First of all, that seems syntactically not correct. Second, in terms of common sense, it should be just a reference. So nothing is actually being copied but the reference (address pointer) to the first array. Which is not what I want. I want to keep the original array intact.

---
---
 
Sorry, I was wrong. I found a good article about array copying:


arr1 = arr2 'this will work! :)

Quote: "We don't need to go any further: Arrays are always copied! You can not have two variables refer to the same array in VBScript!"

But, warning, this will not work with objects:
Set obj2 = obj1
does not copy the object but references obj1 (creates a pointer).
This is not copying.

---
---
 
simple
dim i as integer
dim k as integer

for i=0 to 2
for k=0 to 100
b(k,i)= a(k,i)
next k
next i
 
Remeber arrays start at 0 thats why I started at i=0, if i=0 is empty, put i=1 and same for k. :)
 
Dear ErantD, thanks for taking the pains and answering to my (somewhat old) query, but if you spent an extra minute and clicked the link that I'd provided you would have realized that a1=a2 works perfect for copying arrays. And thus you don't have to do those loops (of course, I was aware of this 'solution'). ---
---
 
Remember though, B = A does not copy the values from array A into array B. What you can do with this is take the "array value" of an array called A and store it into a non-array variant B. Then you can use B almost as if it was an array - though some operations will fail.


Example that fails:
Code:
Dim A(10), B(10)
A(0) = 3
A(9) = 342
B = A
MsgBox CStr(B(9))
B = A can't be used to copy the values in an array A into an array B. That's why the first example dies with a type mismatch.


Example that succeeds:
Code:
Dim A(10), B
A(0) = 3
A(9) = 342
B = A
MsgBox CStr(B(9))
The only difference is that B is not an array in the working example. Of course after the array value of A( ) is stored within it, it can be used almost like an array itself. B is a non-array variable containing an array value inside. Sort of an embedded array.


To prove this, look at this third example:
Code:
Dim A(10), B
A(0) = 3
A(9) = 342
B = A
MsgBox CStr(B(9))
B = 17
MsgBox CStr(B(9))
The first MsgBox will work, but the second one will die, because of a type mismatch where we try to execute B(9) inside of the CStr( ) function. At this point B not only isn't an array, it doesn't contain an array value anymore either!


Finally, look at this one:
Code:
Dim A(10), B
A(0) = 3
A(9) = 342
B = A
MsgBox CStr(B(9))
A = 17
MsgBox CStr(A(9))
The first MsgBox will work as before, but we'll never get to the second one. This script dies with a type mismatch when we try to execute A = 17, because A is an array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top