Override an input vector with user data

Got a question from a Quantlab user today on how to create an input vector that can override another value in another vector that is not manual.

The trick here is to have both value vector and the overriding vector in the same function so you can fetch the size of the original. Also, then you can be sure that when the function runs, both input and potential override will refresh at the same time in the UI.

We want something like in the picture below. See the comments in the code.
The workspace is also attached at the end, ready to run.

option(null: hard); // Throw an error if a null value is used in a non-nullable context

vector(string) ascii_vector(integer start = 65, integer end = 90, logical lowercase = false)
{
	vector(string) ret = s2v(series(k:start,end;str_build([integer(k)])));
	if(lowercase)
		ret = str_to_lower(ret);

	return ret;
}

logical first_eval = true;

// using the "out" in as an input argument will enable it for user input in a table if attached.
out void test_override(out logical reset, out vector(string) res, out vector(string) over)
{
	// on first run or if the reset button is active, we reset the original vector to the alphabet,
	// we also resize and empty the override vector to match the data.
	if(first_eval || reset){
		res = ascii_vector();
		resize(over, v_size(res));
		for(out o:over)
			o = null<string>;
		
		first_eval = false;
		reset = false;
	}

	// we can then loop over two vectors of same lenght.
	// the "out r:res" will make the res vector act by reference
	// this means we dont have to copy any data to an intermediat result vector.
	// we set a value from over only if it is non-null.
	for(out r:res,o:over){
		if(!null(o))
			r = o;
	}
}

Input_vector_that_overrides.qlw (6.8 KB)