Page 1 of 1

Writing a function to move backward through variables

Posted: February 1st, 2015, 3:17 pm
by Keith Tomlin
Hi - this is a little complex to describe, but I'll try. In my questionnaire I have repeated blocks of six questions, and at the end of each block the interviewer may be asked to go back and review incorrect answers. But some of the questions are 'not applicable' as a result of earlier questions, and trying to move into these gives an error message. So I have a piece of code which only moves into a question if isn't not applicable. This works from within a field itself, here's an example for six variables, called FI03_27 to FI03_32 :-

PROC FI03_33
postproc
if FI03_27 <> notappl then move FI03_27
else
if FI03_28 <> notappl then move FI03_28
else
if FI03_29 <> notappl then move FI03_29
else
if FI03_30 <> notappl then move FI03_30
else
if FI03_31 <> notappl then move FI03_31
else
if FI03_32 <> notappl then move FI03_32;
endif;
endif;
endif;
endif;
endif;
endif;

This is a clumsy bit of code, and I need to use it multiple times, so I'd like to put it into a user defined function. Here's an attempt:-

function MoveWithinStaff (alpha Q1, alpha Q2, alpha Q3, alpha Q4, alpha Q5,alpha Q6)
if Q1 <> notappl then move Q1;
else
if Q2 <> notappl then move Q2
else
if Q3 <> notappl then move Q3
else
if Q4 <> notappl then move Q4
else
if Q5 <> notappl then move Q5
else
if Q6 <> notappl then move Q6;
endif;
endif;
endif;
endif;
endif;
endif;

But immediately this gives me an error message at the first if statement:-
ERROR: Invalid character expression near line 55 in GLOBAL procedure

I've quite various things with quotes but it's not made a difference. Any help on where I am going wrong would be very much appreciated.

Many thanks

Keith

Re: Writing a function to move backward through variables

Posted: February 2nd, 2015, 9:56 am
by jashir
It is REALLY COMPLICATED understand you LOADS MAYBE IF YOUR APPLICATION TO THE FORUM

Re: Writing a function to move backward through variables

Posted: February 2nd, 2015, 2:43 pm
by josh
If I understand what you are trying to do then you should be able to make it work with a combination of getsymbol() and getvalue(). Get symbol takes a variable and returns a string containing the name for the variable. So getsymbol(FI03_29) returns the string "FI03_29". The function getvalue() goes the other way, taking the string and returning the value of the variable. Of if the user enters the number 4 for the field FI03_29 then getvalue("FI03_29) will return the number 4.

The problem with your function is that when you pass the variables to your function it is really the numeric values that get passed to it, not the variable names. To fix that you can use getsymbol when calling your function:

Code: Select all

PROC FI03_33
MoveWithinStaff(getsymbol(FI03_27), getsymbol(FI03_28), getsymbol(FI03_29), getsymbol(FI03_30), getsymbol(FI03_31), getsymbol(FI03_32));
That is equivalent to:

Code: Select all

PROC FI03_33
MoveWithinStaff("FI03_27", "FI03_28", "FI03_29", "FI03_30", "FI03_31", "FI03_32");
Once inside the function, you can use getvalue() to get the values to use in the if statements:

Code: Select all

function MoveWithinStaff (alpha Q1, alpha Q2, alpha Q3, alpha Q4, alpha Q5,alpha Q6)
	if getvalue(Q1) <> notappl then 
		move Q1;
	else 
		if getvalue(Q2) <> notappl then 
			move Q2
		else 
			if getvalue(Q3) <> notappl then 
				move Q3
			else 
				if getvalue(Q4) <> notappl then 
					move Q4
				else 
					if getvalue(Q5) <> notappl then
						move Q5
					else 
						if getvalue(Q6) <> notappl then 
							move Q6;
						endif;
					endif;
				endif;
			endif;
		endif;
	endif;
end;
Note that you don't have to do anything special with move statements since move is perfectly happy taking either string or a variable. In other words:
move to "FI03_29"
is equivalent to:
move to FI03_29

Re: Writing a function to move backward through variables

Posted: February 17th, 2015, 1:16 pm
by Keith Tomlin
Many thanks Josh, this solution is working well.

All the best.

Keith