how to use the name of an item in a function

Discussions about CSEntry
Post Reply
thierryt
Posts: 47
Joined: April 26th, 2017, 12:17 pm

how to use the name of an item in a function

Post 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 ?
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

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

Post 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));
thierryt
Posts: 47
Joined: April 26th, 2017, 12:17 pm

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

Post 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 !!!
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

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

Post by Gregory Martin »

Ahh, right! Good fix. I forgot that maxsize is the maximum value, not the width of the maximum value.
Post Reply