Fill array at runtime with values

Joined
Nov 26, 2017
Location
Frankfurt/Germany
Is there a way to fill an already existing array with values at runtime?

Something like

bla[]=[5,4,17,6,4,5,2,3]

Which syntax do I need to use here?

(it should not be a constant, so setting at ONCREATE won't do it, must be during runtime)
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
What you write is close.

If bla doesn't already exist, do this:

Local bla:Int[] = [ 4, 5, 17, 6, 9 ]

If it does exist (or was an unassigned field), do:

bla = [ 4, 5, 17, 6, 9 ]

The original bla will be forgotten and the array above will be assigned to it.

One thing you might be caught out by is if you are trying to do this in a method call. If you pass bla to a method in the parameter list and the method does the second thing above, the original array in the code calling the method won't change. The method only reassigned the bla reference that was passed to it. Instead, the method has to change the elements of bla in place (without resizing), or else return an array that can be assigned to the original bla. Of course if it's a field or global the method can just assign a new array to it.
 
Joined
Nov 26, 2017
Location
Frankfurt/Germany
What I want is to have bla as a global array that I can fill with new values whereever needed.

BTW, bla is a float array. When I do

bla = [1,2,3,4,5]

I get an

Cannot convert from Int[] to Float[]

error.
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
Yes. When Cerberus sees [1,2,3,4,5] it says "that's an int array" so it thinks it can't assign it to a float array (global or otherwise).

Computers are not very good at seeing what you mean!

Single float or int values are automatically converted to the other, so you can write
Local x:Float=5

But for more complex things like int and float arrays, it doesn't automatically understand how to convert them.
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
If the array is the same size, just put the new values in their proper place. If you find it convenient to write the values in array form, you could do something like:

Local newVals:Float[] = [ 1.0, 3.0, 5.0, 7.0, 9.0 ]
For Local i:Int = 0 Until 5
originalArr[ i + 5 ] = newVals[ i ]
Next
 
Joined
Nov 26, 2017
Location
Frankfurt/Germany
What I mean is: let's assume the array is already filled with 100 numbers.
If I want to replace just one of them, I can easily say

bla[46]=5

But if I want to replace position 46,47,48,49,50,51,52 and 53. Is there a way to do that in one single command,
something like

bla[46..53]="5.0,6.0,4.0 ... etc.
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
If you want to do it a lot, write some code to do it!

Code:
Function ReplaceElements:Void( target:Float[], source:Float[], start:Int )
    For Local i:Int = 0 Until source.Length()
        target[ start + i ] = source[ i ]
    Next
End
In your example, you could now write:
ReplaceElements( bla, [ 2.0, 3.0, 7.0, 2.0, 5.0 ], 46 )

Or if you prefer:
Local replace:Float[] = [ 2.0, 3.0, 7.0, 2.0, 5.0 ]
ReplaceElements( bla, replace, 46 )

The above will crash if the source elements run off the end of the target array, but that is probably a good thing if it means you did something wrong.
 
Last edited:
Top Bottom