I would appreciate advice on how to write one dimensional array into two dimensional array.
I have one dimensional array (lets name it aWhole) and the length of this array is known. I need to separate this array in smaller groups so I divide length of one dimensional array with arbitrarily no. of groups and got new array aDivided with the lengths: Width and Groups. Anyone has an idea how to write first array into second one?
Length is not necessary divided with Groups (in that case we round it to get whole no. of Width but idk what we do with the rest part of aWhole).
VARaWhole : ARRAY [0..Main.Length] OF REAL;aDivided : ARRAY [0..Main.Width,0..Main.Groups] OF REAL;END_VAR
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First, you can do the "safe way" with two FOR cycles (assuming Main.Length + 1 = (Main.Width + 1) * (Main.Groups + 1) based on how you declared the two arrays)
FOR i := 0 TO Main.Width DO FOR j := 0 TO Main.Groups DO aDivided[i, j] := aWhole[i * (Main.Groups+1) + j]; END_FOREND_FOR
Second, you can use CAA Memory Library functions like
Hi guys,
I would appreciate advice on how to write one dimensional array into two dimensional array.
I have one dimensional array (lets name it aWhole) and the length of this array is known. I need to separate this array in smaller groups so I divide length of one dimensional array with arbitrarily no. of groups and got new array aDivided with the lengths: Width and Groups. Anyone has an idea how to write first array into second one?
Length is not necessary divided with Groups (in that case we round it to get whole no. of Width but idk what we do with the rest part of aWhole).
You have two options.
First, you can do the "safe way" with two FOR cycles (assuming Main.Length + 1 = (Main.Width + 1) * (Main.Groups + 1) based on how you declared the two arrays)
Second, you can use CAA Memory Library functions like
MEM.MemMove(pSource := ADR(aWhole[0]), pDestination := ADR(aDivided[0,0]), uiNumberOfBytes := Main.Length+1);
Thank you for suggestion. I managed to write 1D array into 2D as needed.
I think you can simply declare both at the same fixed memory address, e.g.
and use whichever you want, a change in one will automatically be reflected in the other.
Last edit: peterned 2022-02-25