Results 1 to 5 of 5

Thread: Broadcast an Array N times

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    100

    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?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    100

    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.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Broadcast an Array N times

    Quote Originally Posted by JohnnyWaffles View Post
    .... 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
  •  



Click Here to Expand Forum to Full Width