Page 1 of 1

how to use the name of an item in a function

Posted: April 28th, 2017, 2:59 pm
by thierryt
Please i want to write a function named edit1, who return for a variable X the result of edit("99….9",X), where the number of 9 is corresponding to the length of item X that is specify in the dictionnary.

Code: Select all

function string edit1(string varname)
	string a = '"' + maketext("%d",maxvalue(varname)) + '"';
	edit1 = edit(a,getvalue(varname));
end;
i wish that if i have for example a numeric variable Q101 in my dictionnary who have len = 5, and if the value of Q101 = 147 in data file,
edit1("Q101") = 00147.
the problem is that when i write maxvalue(varname) the program doesn't understand it is maxvalue(Q101); it doesn't compile.
How can i do please ?

Re: how to use the name of an item in a function

Posted: April 29th, 2017, 11:37 am
by Gregory Martin
Only some CSPro functions, like getvalue, allow you to query the value of an item whose name is stored in a string variable. You can't do that with maxvalue. Your best option is to write this:
function string edit1(string varname,maxsize)

    string formatter = maketext("%s0%dd",'%',maxsize);
    edit1 = maketext(formatter,getvalue(varname));

end;

// call it:
edit1(VALUE,maxvalue(VALUE));

Re: how to use the name of an item in a function

Posted: April 30th, 2017, 8:42 am
by thierryt
Thanks a lot Martin,
it is the solution.
There is just one line to add in the code, the complete code is :

function string edit1(string varname,maxsize)
maxsize = length(maketext("%d",maxsize));
string formatter = maketext("%s0%dd",'%',maxsize);
edit1 = maketext(formatter,getvalue(varname));
end;

//call it
edit1("Q101",maxvalue(Q101));

Thanks !!!

Re: how to use the name of an item in a function

Posted: April 30th, 2017, 10:43 am
by Gregory Martin
Ahh, right! Good fix. I forgot that maxsize is the maximum value, not the width of the maximum value.