Calling commands and functions from a variable
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Calling commands and functions from a variable
I am designing a system for computer assisted phone surveys. Here is the flow of events:
-user connects to a database and downloads a specific survey which is converted into an array
-the first question is displayed in a field, the possible responses are displayed in a select box, and finally we have a button for "Next"
-when the user presses next, the response is recorded, and the next question is displayed
So I needed to be able to add the ability to skip questions based on answers. So in the survey construction section of the program, the administrator can edit a livecode script ("on mouseUp") which helps determine the appropriate question to ask next. This script is saved in the database along with the questions. When the user retrieves the survey from the database, it sets the script of button "Next" to the script from the database.
What I would prefer to do is save this logic scripting in some other way and leave the script of "Next" alone. I would prefer to have "Next" call commands and functions from another source. Can I use a script right out of a variable?
-user connects to a database and downloads a specific survey which is converted into an array
-the first question is displayed in a field, the possible responses are displayed in a select box, and finally we have a button for "Next"
-when the user presses next, the response is recorded, and the next question is displayed
So I needed to be able to add the ability to skip questions based on answers. So in the survey construction section of the program, the administrator can edit a livecode script ("on mouseUp") which helps determine the appropriate question to ask next. This script is saved in the database along with the questions. When the user retrieves the survey from the database, it sets the script of button "Next" to the script from the database.
What I would prefer to do is save this logic scripting in some other way and leave the script of "Next" alone. I would prefer to have "Next" call commands and functions from another source. Can I use a script right out of a variable?
Greg (pink) Miller
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
Re: Calling commands and functions from a variable
This is going to be good.
I think that allowing modification of a handler is fraught with peril. Anything a human can do to determine what the question "flow" ought to be can be done better under script control. I wrote self-modifying code back in HC days. I never needed to, I just did for the exercise. That capability is within LC as well, since scripts are properties of objects.
Don't. And unless I am misreading your intent, don't let a person do such a thing either.
So what in the nature of your question/answer flow would make you want to (need to?) change a handler? I bet you do not have to. Can you give a short example that you are thinking fits this outlandish idea?
Craig Newman
I think that allowing modification of a handler is fraught with peril. Anything a human can do to determine what the question "flow" ought to be can be done better under script control. I wrote self-modifying code back in HC days. I never needed to, I just did for the exercise. That capability is within LC as well, since scripts are properties of objects.
Don't. And unless I am misreading your intent, don't let a person do such a thing either.
So what in the nature of your question/answer flow would make you want to (need to?) change a handler? I bet you do not have to. Can you give a short example that you are thinking fits this outlandish idea?

Craig Newman
Re: Calling commands and functions from a variable
here is a real example from a survey currently in the field, and it is quite simple
this one only skips around, but I have other logic built around quotas, text replacement and randomization of subsets of questions as well
the current question is put into the variable "currentQ"
each question has a default question to go to gMaster["survey"][currentQ]["next"]
each question has a logic group it is assigned to gMaster["survey"][currentQ]["logic"]
if the question has no need for logic (that is, it always goes to the "next" question) then the logic group is "0"
so the script below is programmed by an administrator, and included in the database record when saved
when the user calls the survey, the following line of code is used:
set the script of button "nextQuestion" on card "Interview" to gMaster["survey"]["logicscript"]
this one only skips around, but I have other logic built around quotas, text replacement and randomization of subsets of questions as well
the current question is put into the variable "currentQ"
each question has a default question to go to gMaster["survey"][currentQ]["next"]
each question has a logic group it is assigned to gMaster["survey"][currentQ]["logic"]
if the question has no need for logic (that is, it always goes to the "next" question) then the logic group is "0"
so the script below is programmed by an administrator, and included in the database record when saved
when the user calls the survey, the following line of code is used:
set the script of button "nextQuestion" on card "Interview" to gMaster["survey"]["logicscript"]
Code: Select all
global gMaster
on mouseUp
put saveAnswer() into temp
if temp is false then exit mouseUp
put field "qLabel" into currentQ
put getCodes(currentQ) into tCode
switch gMaster["survey"][currentQ]["logic"]
case "0" --the default condition
displayQuestion gMaster["survey"][currentQ]["next"]
break
case "q2skip" --if q2 is "2" ends the survey
if tCode is 2 then displayQuestion "zanks"
else
displayQuestion put gMaster["survey"][currentQ]["next"]
end if
break
case "q8skip" --if q8 is 0-2 ask q8a, if 9-10 ask q8b, otherwise jump right to the default next question
if tCode <= 2 then displayQuestion "q8a"
else if tCode >=9 then displayQuestion "q8b"
else
displayQuestion gMaster["survey"][currentQ]["next"]
end if
end switch
end mouseUp
Greg (pink) Miller
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
Re: Calling commands and functions from a variable
Hi.
But I suspect that a much more direct and straightforward methodology will do...
Craig
I am not seeing where or why anyone would or should change a handler. I certainly understand calling different handlers based on user choices, or any other system states. Probably just me.so the script below is programmed by an administrator,
But I suspect that a much more direct and straightforward methodology will do...
Craig
Re: Calling commands and functions from a variable
Here's a few more elements that may make this a bit clearer.
1. The administrators themselves are not going to be programmers, nor will they have access to the development environment. Ability to program the logic sequences needs to be self contained within the application.
2. I'm also working on means by which I can create more handlers to do a few tasks and to make and even simpler language... for example
will become:
3. So I either need to be able to execute a foreign script, or I need to create a framework by which I can execute an unknown number of commands with an unknown number of parameters... I've taken a swing at the latter and haven't quite gotten there yet.
Any suggestions?
1. The administrators themselves are not going to be programmers, nor will they have access to the development environment. Ability to program the logic sequences needs to be self contained within the application.
2. I'm also working on means by which I can create more handlers to do a few tasks and to make and even simpler language... for example
Code: Select all
case "q8skip" --if q8 is 0-2 ask q8a, if 9-10 ask q8b, otherwise jump right to the default next question
if tCode <= 2 then displayQuestion "q8a"
else if tCode >=9 then displayQuestion "q8b"
else
displayQuestion gMaster["survey"][currentQ]["next"]
end if
break
Code: Select all
case "q8skip"
ifAnswerLTE 2,"q8a"
ifAnswerGTE 9,"q8b"
break
Any suggestions?
Greg (pink) Miller
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
Re: Calling commands and functions from a variable
The unknown number of parameters is less daunting to me than an unknown number of commands, The first is straightforward, as long as the number of parameters does not exceed a prepared ability to process them.
But I stil do not know what can be done about an unknown "number of commands". The usual method is to have one or more fixed handlers that are called as needed, based on other environmental conditions, with parameters at the ready to allow those handlers to do their thing. But you cannot easily create a handler based on a certain set of conditions, if that is even what you are driving at.
Can you give an example of a case where a single handler and any number of parameters will not do, that you need to write a different handler based on the nature of the case?
Craig
But I stil do not know what can be done about an unknown "number of commands". The usual method is to have one or more fixed handlers that are called as needed, based on other environmental conditions, with parameters at the ready to allow those handlers to do their thing. But you cannot easily create a handler based on a certain set of conditions, if that is even what you are driving at.
Can you give an example of a case where a single handler and any number of parameters will not do, that you need to write a different handler based on the nature of the case?
Craig
Re: Calling commands and functions from a variable
The following example is based on a real survey I had to program once. I changed the product to beer due to confidentiality agreements, but otherwise it is quite faithful to the original.
Here are 6 questions from a survey, they all have the same answers listed below
Q4 When you think of Stouts or Porters what brand comes to mind first? (List-Single choice)
Q6 What others come to mind? (Multiple choice)
Q7 Which have you drunk in the past 3 months? (Multiple choice)
Q15 Which Stout or Porter have you seen advertisements for in the past 3 months? (Multiple choice)
Q19 Which Stout or Porter have you seen in promoted in pubs/bars in the past 3 months? (Multiple choice)
Q22 (only asked if Q21=2) What Stout or Porter is your favorite? (List-Single Choice)
Answer List:
1 Guinness Draught
2 Murphy's Stout
3 Samuel Adams Imperial Stout
4 Beamish Stout
5 Courage Imperial Russian Stout
6 Marston's Oyster Stout
7 Rouge Ale's Chocolate Stout
8 Porterhouse Brewing Company Plain Porter
9 Don't remember
10 Other (Specify)
There are 8 different series of questions related to the 8 brands above, each series has 12 questions.
The respondents will be assigned 2 based on the following criteria:
1. If Guinness (1) is mentioned in any question then this is 1 of the brands
2. Response to Q22 if it is not Guinness or Don't Know, if not....
3. If Murphy's (2) Sam Adams (3) or Beamish (4) are mentioned in Q4 Q6 Q7 Q15 or Q19, randomly select one, if not...
4. Randomly select another brand mentioned in Q4 Q6 Q7 Q15 or Q19 (except for Don't know or Other)
I'm not saying this couldn't be crammed into a single handler somehow... I just don't have a clue how I would.
Here are 6 questions from a survey, they all have the same answers listed below
Q4 When you think of Stouts or Porters what brand comes to mind first? (List-Single choice)
Q6 What others come to mind? (Multiple choice)
Q7 Which have you drunk in the past 3 months? (Multiple choice)
Q15 Which Stout or Porter have you seen advertisements for in the past 3 months? (Multiple choice)
Q19 Which Stout or Porter have you seen in promoted in pubs/bars in the past 3 months? (Multiple choice)
Q22 (only asked if Q21=2) What Stout or Porter is your favorite? (List-Single Choice)
Answer List:
1 Guinness Draught
2 Murphy's Stout
3 Samuel Adams Imperial Stout
4 Beamish Stout
5 Courage Imperial Russian Stout
6 Marston's Oyster Stout
7 Rouge Ale's Chocolate Stout
8 Porterhouse Brewing Company Plain Porter
9 Don't remember
10 Other (Specify)
There are 8 different series of questions related to the 8 brands above, each series has 12 questions.
The respondents will be assigned 2 based on the following criteria:
1. If Guinness (1) is mentioned in any question then this is 1 of the brands
2. Response to Q22 if it is not Guinness or Don't Know, if not....
3. If Murphy's (2) Sam Adams (3) or Beamish (4) are mentioned in Q4 Q6 Q7 Q15 or Q19, randomly select one, if not...
4. Randomly select another brand mentioned in Q4 Q6 Q7 Q15 or Q19 (except for Don't know or Other)
I'm not saying this couldn't be crammed into a single handler somehow... I just don't have a clue how I would.
Greg (pink) Miller
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
Re: Calling commands and functions from a variable
OK. Whew.
One handler.
I don't know about cramming, but this will be FAR simpler to author and easier to maintain than the multi-ad hoc handler suite you were first thinking about.
The devil is in the collection of the data based on the myriad possible array of responses, but not at all in the construction of the gadget itself. The answers need to be encoded, perhaps in an array, but that will be a matter of style. We can work through this easily and quickly.
You mentioned that certain responses might branch out of or change the order or number of questions in any series. Is this so?
Are the answers presented in a list of some kind?
Are the questions presented in a list, or a series of dialogs? Something else?
Craig
One handler.
I don't know about cramming, but this will be FAR simpler to author and easier to maintain than the multi-ad hoc handler suite you were first thinking about.
The devil is in the collection of the data based on the myriad possible array of responses, but not at all in the construction of the gadget itself. The answers need to be encoded, perhaps in an array, but that will be a matter of style. We can work through this easily and quickly.
You mentioned that certain responses might branch out of or change the order or number of questions in any series. Is this so?
Are the answers presented in a list of some kind?
Are the questions presented in a list, or a series of dialogs? Something else?
Craig
Re: Calling commands and functions from a variable
Hi Pink:
I am going to assume that only the question sequence is dynamically determined, not the logic (that is, logic is pre-built into the program). If so, here's one way.
Say, for question N, the user chooses the answer K.
Let the program look for a command called, say, qNaK; if present, it calls the command, which assigns the next question number.
If there isn't a command, the next default question is chosen to be presented to the user.
So, the script of the "Next" button would be
Thus you need to code functions for just the exceptions to default. These can be as complex as you need, including randomization, and logic dependent on the sequence of questions already answered.
I am not sure this answers your needs (e.g., a single handler), but here it is anyway.
Regards,
Sri
I am going to assume that only the question sequence is dynamically determined, not the logic (that is, logic is pre-built into the program). If so, here's one way.
Say, for question N, the user chooses the answer K.
Let the program look for a command called, say, qNaK; if present, it calls the command, which assigns the next question number.
If there isn't a command, the next default question is chosen to be presented to the user.
So, the script of the "Next" button would be
Code: Select all
on mouseUp
put "command" && "q" & tQuestionNum & "a" & tAnswerNum into tCommandName
if the script of this stack contains tCommandName then
send tCommandName to this stack
else
put N + 1 into gNextQuestionNum
end if
end mouseUp
-- in stack script, a series of commands such as this
command q1a3
-- do your logic thing
-- put the result into the global gNextQuestionNum
end q1a3
I am not sure this answers your needs (e.g., a single handler), but here it is anyway.
Regards,
Sri
Re: Calling commands and functions from a variable
A couple of responses have slipped through.
I don't know if my suggestions makes sense in light of new info.
Sri.
I don't know if my suggestions makes sense in light of new info.
Sri.
Re: Calling commands and functions from a variable
yes, responses do require branching and/or changing the orderdunbarx wrote:OK. Whew.
One handler.
I don't know about cramming, but this will be FAR simpler to author and easier to maintain than the multi-ad hoc handler suite you were first thinking about.
The devil is in the collection of the data based on the myriad possible array of responses, but not at all in the construction of the gadget itself. The answers need to be encoded, perhaps in an array, but that will be a matter of style. We can work through this easily and quickly.
You mentioned that certain responses might branch out of or change the order or number of questions in any series. Is this so?
Are the answers presented in a list of some kind?
Are the questions presented in a list, or a series of dialogs? Something else?
Craig
there is a locked field on the card which displays the question text, this is updated when the interviewer clicks "Next"
there is a scrolling listing field on the card which displays the responses, the interviewer selects one (or more if it is multiple response) before clicking next
there is also a field which displays the question number
essentially, these 3 fields and the next button make up the entire interface for survey administration (there's also a button called "STOP" which terminates the survey before finishing)
screen shot attached
Greg (pink) Miller
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
Re: Calling commands and functions from a variable
OK.
Most of the action will take place in the "Next" button. This will update the question number, load the question and answer fields in sequence, and log the responses. It will make the decisions on the proper next question based on rules that you will hard wire. You then can sift through that data and collect what you need. But far more important...
Where are you with LC? How confident are you to undertake this task, which is of an intermediate level? In other words, you need a solid foundation with LC basics, or you will not be able to do it at all. We will provide all the support you ask for, but that will not get the app done.
Craig
Most of the action will take place in the "Next" button. This will update the question number, load the question and answer fields in sequence, and log the responses. It will make the decisions on the proper next question based on rules that you will hard wire. You then can sift through that data and collect what you need. But far more important...
Where are you with LC? How confident are you to undertake this task, which is of an intermediate level? In other words, you need a solid foundation with LC basics, or you will not be able to do it at all. We will provide all the support you ask for, but that will not get the app done.
Craig
Re: Calling commands and functions from a variable
I think I have a reasonably solid foundation, which isn't to say I'm not constantly finding commands and functions that I never noticed before...
Let me show you what I've got thus far...
so in the logic portion for example, I could have the following:
displayif,greaterequal,9,q8b
displayif,lessequal,2,q8a
(and then it would go to q9 by default if neither of the above are true)
Let me show you what I've got thus far...
Code: Select all
command logicTriggers
put field "qLabel" into currentQ
put gMaster["survey"][currentQ]["logic"] into tLogic
set itemdel to comma
repeat for each line tCmd in tLogic
put item 1 of tCmd into tBaseLog
switch tBaseLog
case "0"
displayQuestion gMaster["survey"][currentQ]["next"]
break
case "displayIf"
displayIf (item 2 of tCmd),(item 3 of tCmd),(item 4 of tCmd)
break
end switch
end repeat
displayQuestion gMaster["survey"][currentQ]["next"]
end logicTriggers
command displayIf pOperator pVal pNext
put field "qLabel" into currentQ
put getCodes(currentQ) into tCodeList
set itemdel to comma
repeat for each item tCode in tCodeList
switch pOperator
case "equal"
if tCode = pVal then
displayQuestion pNext
exit to top
end if
break
case "notequal"
if tCode <> pVal then
displayQuestion pNext
exit to top
end if
break
case "greaterequal"
if tCode >= pVal then
displayQuestion pNext
exit to top
end if
break
case "lessequal"
if tCode <= pVal then
displayQuestion pNext
exit to top
end if
break
case "greater"
if tCode > pVal then
displayQuestion pNext
exit to top
end if
break
case "less"
if tCode < pVal then
displayQuestion pNext
exit to top
end if
break
end switch
end repeat
end displayIf
displayif,greaterequal,9,q8b
displayif,lessequal,2,q8a
(and then it would go to q9 by default if neither of the above are true)
Greg (pink) Miller
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
Re: Calling commands and functions from a variable
OK.
The script you posted shows that you are indeed more than capable of doing this. Great.
There are several places you could tighten your code, but that is not important here at all. Get the thing running...
Craig
The script you posted shows that you are indeed more than capable of doing this. Great.
There are several places you could tighten your code, but that is not important here at all. Get the thing running...
Craig
Re: Calling commands and functions from a variable
I'm running through comparisons in my head that I need to make.
The simplest is the example I posted earlier, a comparison of the current question's answer with a specific value.
I also need to make these kind of a comparisons (exhausting, but not exhaustive list)
-current question's answer with a previous question's answer e.g. Q1 = Q2
-a set of questions' answers and values e.g. (Q1 =2 and Q2 <4) or (Q1>3 or Q2 >=7)
-the current question and a value from the source record (Q2=5 and age>30 and gender="male")
-a count of occurrences of a specific value across questions e.g. (the number of times "1" is chosen in Q1 to Q5)
-current question's answer and the current count of a quota e.g. (q2=1 and (count_q2 < quota_q2))
and of course, all the above can compound
The simplest is the example I posted earlier, a comparison of the current question's answer with a specific value.
I also need to make these kind of a comparisons (exhausting, but not exhaustive list)
-current question's answer with a previous question's answer e.g. Q1 = Q2
-a set of questions' answers and values e.g. (Q1 =2 and Q2 <4) or (Q1>3 or Q2 >=7)
-the current question and a value from the source record (Q2=5 and age>30 and gender="male")
-a count of occurrences of a specific value across questions e.g. (the number of times "1" is chosen in Q1 to Q5)
-current question's answer and the current count of a quota e.g. (q2=1 and (count_q2 < quota_q2))
and of course, all the above can compound
Greg (pink) Miller
MadPink, LLC
I'm Mad, Pink and Dangerous to Know
MadPink, LLC
I'm Mad, Pink and Dangerous to Know