Multiplying Array with single value


I am stuck with a very basic problem. I need to multiply one value with an array.

A = 5.0
B= Array(1,2,…)

C= A*B

I am getting error Type mismatch. Is there any library in Rhinoscript which I can use to perform such operations.

Thanks for help.

Python will let you do multiply a list by a constant, but vb will not directly. So you need to run a loop and multiply each element:

In-place multiplication

Dim A, B, i
For i=0 to Ubound(B)

Copy array

Dim A, B, C, i
For i=0 to Ubound(B)


Dim A, B, C(), i
Redim C(Ubound(B))
For i=0 to Ubound(B)


Thank you @Helvetosaur. I was hoping vbscript already has such functions built in. But this way works too.

Here’s the function I have created to multiply variable A with array B.

Function Multiply(A, B())
Dim i
Dim mult()
Dim temp
For i=0 To UBound(B)
	temp = A * B(i)
	ReDim Preserve mult(i)
	mult(i) = temp
Multiply = mult
End Function

I am having another problem with this code though.

Dim Y()

'Setting array dimension for Y
ReDim Y(1)
Beam = 20
B = Array(0.0, 0.5)	
Y = Multiply(Beam, B)

This code is giving error message "Type mismatch" at Y=Multiple(Beam,B). If I dont set Y as a dynamic array, then it works. But I need to use Y multiple times in my code where the size of Y will change.

What is the right way of setting an array to another array variable of same size?

What you can do with dynamic arrays is pretty limited in vbscript. If an array has been defined as dynamic, you can’t redefine it as fixed. But in this case, it doesn’t seem you need it. If Y is not defined as a dynamic array, setting Y=something will always work, even if something is a different sized array from what Y was previously - or even not an array at all, but a string, number etc…

If you’re looking to do extensive array (list) operations, I would highly recommend switching to Python, which has tremendous tools for working with lists.


A post was split to a new topic: Simple Addition Array