-
Jul 19th, 2021, 04:40 PM
#1
Thread Starter
Lively Member
Broadcast an Array N times
I feel like I have been coding for too long this afternoon, because for the life of me, I can't find a solution to what seems to be a simple problem.
I have an array that I need to extend multiple times over.
Dim dz() as Double
Redim dz(1 to 10)
I have the variable dz() full of 10 random numbers. I need to copy that number sequence (1 through 10) 10 times into another array.
Python does this quite easily, but I'm struggling to figure it out in VBA. Any thoughts on how you can do this?
-
Jul 19th, 2021, 04:56 PM
#2
Re: Broadcast an Array N times
Is the other array a 1d array with 100 elements? Or a 2d 10x10 array?
Code:
'1d pcode
for i = 1 to 100
otherArray(i) = dz(i mod 10)
next i
Code:
'2d pcode
for i = 1 to 10
for j = 1 to 10
otherArray(i, j) = dz(j)
next j
next i
-
Jul 20th, 2021, 08:33 AM
#3
Re: Broadcast an Array N times
Of course, that code is assuming that the destination array is pre-dimensioned to the size (or more) that you need.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jul 20th, 2021, 10:20 AM
#4
Thread Starter
Lively Member
Re: Broadcast an Array N times
OptionBase1, thank you for the suggestion. I did not think of doing that. It works great. I had to add some extra code to actually get the 10th value in dz(), because (i mod 10) evaluates to 0 when i is divisible by 10. Easy enough though with your help getting me started. Thanks again!.
And yea the destination array is pre-dimensioned passel.
-
Jul 23rd, 2021, 09:37 AM
#5
Re: Broadcast an Array N times
Originally Posted by JohnnyWaffles
.... I had to add some extra code to actually get the 10th value in dz(), because (i mod 10) evaluates to 0 when i is divisible by 10. ...
That is why it is usually more useful to use arrays that start at 0 rather than 1, if you need to use Mod and other operations that will return 0.
But, to turn a 0 based index into 1 based index should have been a simple matter of adding 1 to the Mod result, so not need much extra code.
Code:
otherArray(i) = dz((i mod 10) + 1)
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|