extract the nth value from a valueset and fill a field with this value

Discussions about CSEntry
Forum rules
New release: CSPro 8.0
Post Reply
SKARA
Posts: 15
Joined: September 13th, 2012, 7:09 am

extract the nth value from a valueset and fill a field with this value

Post by SKARA »

Hi everybody,

How can i extract the nth value from a valueset and fill a field with this value.
For example, if FunctionExtractNthVs is this function and the following values are the possible values of a Q1 item :
A = Senegal
B = Gambia
C = Ghana
D = France
E = Japan.
I would like the FunctionExtractNthVs function to retrieve the 3rd value (Ghana) such as FunctionExtractNthVs (Q1,3) = Ghana.

Regards,
KARABENTA SIDIKI
Gregory Martin
Posts: 1777
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: extract the nth value from a valueset and fill a field with this value

Post by Gregory Martin »

The only way to get the labels from a value set is to use the getlabel function. This works fine if you have a value set with consecutive discrete values, but if you have gaps in data (like you skipped D and France was E), then this will be a problem for you.

I could get Ghana with code like this:
getlabel(VALUE_SET_NAME, "C");
Or:
string alphabet = "ABCDE...";

getlabel(VALUE_SET_NAME, alphabet[3:1]);
SKARA
Posts: 15
Joined: September 13th, 2012, 7:09 am

Re: extract the nth value from a valueset and fill a field with this value

Post by SKARA »

Gregory Martin wrote:The only way to get the labels from a value set is to use the getlabel function. This works fine if you have a value set with consecutive discrete values, but if you have gaps in data (like you skipped D and France was E), then this will be a problem for you.

I could get Ghana with code like this:
getlabel(VALUE_SET_NAME, "C");
Or:
string alphabet = "ABCDE...";

getlabel(VALUE_SET_NAME, alphabet[3:1]);
=========================================================
Hi Gregory,

Thank you very much for your quick reply.
My problem is that the valueset is randomized and the codes occupy different positions each time. And I want each time to extract the value occupying a given rank n after the randomization (for example the value occupying the rank 3).
But already, your second proposal gave me an idea that I will exploit.
=========================================================
Otherwise, here is the overall problem:
I have a Checkbox question for which I want to randomize the valueset in preproc and automatically fill item with all of the values in the valueset in this random order. Is it possible in CsPro ?


Regards
KARABENTA SIDIKI.
Gregory Martin
Posts: 1777
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: extract the nth value from a valueset and fill a field with this value

Post by Gregory Martin »

Does the randomizevs function work for your purposes?

Also, it would be some work, but you could move the values of your non-continuous value set into an array doing something like this:
numeric array_counter = 1;

do numeric value = minvalue(VALUE_SET_NAME) while value <= maxvalue(VALUE_SET_NAME)

    string value_label = getlabel(VALUE_SET_NAME, value);

    if value_label <> "" then // skip over values that aren't defined

        value_set_array(array_counter) = value_label;
        inc(array_counter);

    endif;

enddo;

// now, you can get the third element in the value set by using value_set_array(3)
Minvalue/maxvalue work on numeric value sets. If you have an alpha value set, you'll have to iterate in another way.
Post Reply