How to search and replace in 2D array

I have a large 2D array with values that have $ sign in in. Is there a method to replace them without looping through every element? Similar to method of ReplaceAll for range of cells

Well…it depends on what you want. Since you have the question tagged with BASIC then you can absolutely do the replacement using SUBSTITUTE without looping, but what you get back won’t be a 2D array, it will be nested arrays…that is not A(1,1), etc., but A(1)(1).

I don’t know what your motivation is for not looping. Programs loop; that’s what they do. :upside_down_face: But if you are concerned about speed, the underlying substitution being done by SUBSTITUTE might be a performance gain over doing the substitution in BASIC. Still, if you need a 2D array as the result, you’ll have to do the looping to work out the array structure…but it is very tidy looping.

The attached ODS has both a looped substitution in LO BASIC and one in LO BASIC using SUBSTITUTE then correcting the nested array into a 2D array. You can experiment for speed, I suppose, if either meets your need. Notice that there aren’t any safeguards in place.

The basic idea is to use FunctionAccess then convert the nested result to 2D within a loop structure.

Replacementator.ods (10.3 KB)

Thanks Joshua, a very clean function. Looks like the one with loop is even more clean :slight_smile: so I’ll take this one and since it’s a function it doesn’t clutter my code.
I was just thinking there would be some method (one liner) that would quickly replace like there is SF_Array.ImportFromCVSFile or SF_Calc.SetArray or ReplaceAll which all work very quick with arrays with no need to loop through each line or cell in the file.