08-30-2022 07:20 AM
If I have a 2D uninitialized string array and want to write a str to let say cell 5.2. How should I do it?
Further, let say I want to add more data into the array at cell 7,5. How should that be done?
(The empty cells can be filled with Empty String Const.)
Solved! Go to Solution.
08-30-2022 07:57 AM
You can use the "Max & Min" function to determine easily the needed new array size:
If needed, you create a new array with the needed size and copy the original part into the new larger array. Then replace the new element at the given index position.
08-30-2022 07:58 AM
I believe the simple answer to your question is that you cannot manipulate "what isn't there", meaning you need to first initialize your String Array (which sets its size, and lets you define what you mean by "Cell 7, 5" (does this mean the 6th element in the 8th row, considering the initial element is at (0, 0)?).
Bob Schor
08-30-2022 08:10 AM
Some additional hints:
if your array is of significant size and you want to insert many elements at outer ranges, it is better to initialize your array to the max assumed size, insert the new elements with the "In Place Element Structure" and shrink the array afterwards (Subarray).
08-30-2022 08:47 AM - edited 08-30-2022 08:48 AM
You confirm the method i use. It is similar as mine. I add with Insert array a subarray below or to the right of the last free row/col.
I have also add code that detect first free row/col and use that value as xy-parameter for the new data. I did that for fun.
08-30-2022 08:56 AM
Me too 😁
08-30-2022 11:19 AM
@TakeANap wrote:
You confirm the method i use. It is similar as mine. I add with Insert array a subarray below or to the right of the last free row/col.
I have also add code that detect first free row/col and use that value as xy-parameter for the new data. I did that for fun.
For Insert Into Array, if you leave the index inputs unwired, it automatically adds the new data to the end. No need to get array size and wire the inputs.
Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.
08-30-2022 03:17 PM - edited 08-31-2022 09:01 AM
@TakeANap wrote:
I add with Insert array a subarray below or to the right of the last free row/col.
I have also add code that detect first free row/col and use that value as xy-parameter for the new data. I did that for fun.
Your "method" requires significantly more code because you need to determine if a subarray needs to be inserted below, to the right, or even both. I don't understand what the "fun" in that would be. A Rube Goldberg cartoon perhaps? 😄 Dave's solution is significantly better and more efficient.
The correct solution would be to decide from the beginning on a reasonable maximum size, initialize an array of empty strings at that size and keep the size fixed. Memory management is significantly better if arrays don't constantly change size. Also the code is greatly simplified.
Note that arrays of any dimension are stored linearly in contiguous memory, so inserting columns typically requires a complete new allocation for everything because most elements need to be moved to new positions relative to the first element (have a look at slide #11 here (in part II)).
You did not explain the purpose of all this! For example if the 2D array is typically "sparse" (only very few elements are not empty strings), you might want to use a MAP with an array of indices as key and a string as value. Now you can even add an element at Index [2147483647, 2147483647] without much memory penalty and can look it op at any time later. All elements that are empty strings are default and don't need to be part of the map. It is safe to say that a 2D array of empty strings with 10^18 elements is not possible. You could even make the "key" a pair of U64 to perfectly store an string value with indices 18446744073709551615 each. 😄
08-31-2022 12:36 AM
Your comment was exactly what I was looking for when I posted my question. I will look into your suggestions.
The VI I'm speaking about isn't used in a time-critical application. However, if I would be writing code for ha highspeed sampling engine I would have used another way of allocating memory and storing data. I working also with embedded microcontrollers and forget that PC:s has lots of memory. 🙂