problems scoring selections in a form

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

problems scoring selections in a form

Post by p0ntif » Wed Jul 02, 2014 6:22 pm

tabulating survey data.

Hello all, I was hoping someone could help me with my dilemma:

I have created a very brief 10 question survey and want to tabulate the answers.
The survey consists of 9 questions which cycle to the next question after each selection and there are 4 radio buttons as a group (each radio button should be worth 0-3 points).
What I am having trouble with is tabulating the numerical value of each survey response and returning it at the end in the field "Score".

I have declared a variable qTab and on each radio button have included the following object script:

Code: Select all

on mouseUp
   global qTab
   
   evaluate(the short name of me)
   qTab +1 --it is qTab +2, and qTab +3 for the other 2 buttons of value.  
end mouseUp
It returns an error at the qTab part. i assume I am not doing this correctly. I have declared the global variable qTab in the card script and have it return the value of qTab into a field as so (this is a segment of the larger script):

Code: Select all

On openCard
   global qTab
   nextQuestion
   put qTab into fld "Score"
end openCard

command nextQuestion
   global qNbr, qArray
   put qArray[qNbr]["question"] into fld "Question"
end nextQuestion
If someone can graciously point out to me what my error is with trying to tabulate this data it would be much appreciated. thank you!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: problems scoring selections in a form

Post by dunbarx » Wed Jul 02, 2014 6:48 pm

Hi.

Well, you are just not quite up to speed with the syntax and structure of LC. Your first foray is ambitious, but you are tripping on small but fatal details.

A couple of questions, which will be the subject of several small discussions. And before we start, I can tell you now that we will find a much simpler and more straightforward method than whatever you have attempted. Not that this is a bad thing. Practice, in the beginning, is more important than results. You may not believe that now, but you will.

-- Have you named your radio buttons with a simple number?
-- The syntax " qTab +1" is invalid. I think I know what you are after, but no.
-- You do not need, or want, globals.
-- Why arrays?
-- There is much more.

How are the survey questions presented? However they are, you are nearly done (or can be) at that point. Is a question presented, and the user clicks on a radio button? In that case, you are in fact done. Write back, addressing the above points. We will fix this. And then, maybe for homework, we will try to figure out that other stuff you tried on your own.

Craig Newman

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: problems scoring selections in a form

Post by jacque » Wed Jul 02, 2014 6:53 pm

You're treating a variable like a handler call. When LC sees "qTab" it is looking for a handler of that name. If you only want to increase the value of the variable:

add 1 to qTab

Also, you don't show a function in your post named "evaluate" but you'll get an error there too if one doesn't exist. What is the evaluation supposed to return? The handler doesn't do anything with the return value, and the syntax isn't correct for a function, which requires a container to hold the returned value. If it is supposed to be a command handler rather than a function handler, the parentheses shouldn't be there.

It looks like you may be coming from a C language variant. LC syntax is more verbose and quite a bit different, but after some adjustment you'll come to love it. Have you looked at the user guide? There are also these stacks which lead you through the basics:

http://www.hyperactivesw.com/revscriptc ... ences.html
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Re: problems scoring selections in a form

Post by p0ntif » Wed Jul 02, 2014 8:05 pm

First of all, thank you for the replies. I am certainly in the complete beginner format and am doing my best, by well reading, and doing. But as with probably the most of us, I learn best by failing and learning what doesn't work. It isn't the most efficient way to learn by any means, but when I get it, i get it. ;) With that aside, I will move on to your questions:
dunbarx wrote: -- Have you named your radio buttons with a simple number?
there are 4 radio buttons that are grouped for the purpose of only clicking on one for each presented question.
I want each of the radio buttons to be worth 0-3 points So r1 = 0, r2= 1, r3 =2, r4 =3 (as an example)
I have named each radio button separately: (r1, r2, r3, r4)
-- The syntax " qTab +1" is invalid. I think I know what you are after, but no.
-- You do not need, or want, globals.
-- Why arrays?
-- There is much more.
I figured it was a syntax error, but I am just not sure how to go about doing this simple arithmatic and putting it all into a summated variable yet.
The array is for the first sgment of the code, which holds the question numbers and questions. Maybe this is too much?
How are the survey questions presented? However they are, you are nearly done (or can be) at that point. Is a question presented, and the user clicks on a radio button? In that case, you are in fact done.
Exactly right. They populate a predesignated field and the radio button group remains the same for each question. Exactly as you portrayed. Thank you in advance for your help!

p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Re: problems scoring selections in a form

Post by p0ntif » Wed Jul 02, 2014 8:10 pm

jacque wrote:You're treating a variable like a handler call. When LC sees "qTab" it is looking for a handler of that name. If you only want to increase the value of the variable:

add 1 to qTab


is it really that simple?
Also, you don't show a function in your post named "evaluate" but you'll get an error there too if one doesn't exist. What is the evaluation supposed to return? The handler doesn't do anything with the return value, and the syntax isn't correct for a function, which requires a container to hold the returned value. If it is supposed to be a command handler rather than a function handler, the parentheses shouldn't be there.
Yes, evaluate is a function that kind of turns the pages for each question and was not part of the program code that I posted.
It looks like you may be coming from a C language variant. LC syntax is more verbose and quite a bit different, but after some adjustment you'll come to love it. Have you looked at the user guide? There are also these stacks which lead you through the basics:
It has been more than a decade when I was an amateur working with coding, and am submitting myself back into the foray. I got in on the kickstarter for this and a year later decided to finally take the plunge back in. I am happy to receive recommendations on any resources you can identify that may be of help. Thanks!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: problems scoring selections in a form

Post by jacque » Wed Jul 02, 2014 9:03 pm

You're doing fine, and your buttons are named correctly. LC doesn't like the names of objects to be plain numbers (ask us why if you're curious) so including at least one alpha character is always safest. But here's a little trick for your naming scheme:

Name your radio buttons "r" and a space and a number that represents their value. So instead of r1, r2, etc. you'd have "r 0", "r 1", "r 2" and "r 3". This lets you look at the last "word" of the name and it will always be the number of points that you want to assign to the answer. A "word" in LC is any text separated by spaces, so the last word of "r 0" is the zero. Now your script can do this:

Code: Select all

put the last word of the short name of me into tValue -- assign, for example, zero to the variable tValue
add tValue to qTab
Or you can do it in one line:

Code: Select all

add the last word of the short name of me to qTab -- we can do this in LC because the engine converts text to numbers as needed
You'd use that if you put a script into every button. But that's not the most efficient way, it's better to have a single handler that catches all radio button clicks, or else assign a behavior script to each radio button. Either way allows you to edit a single handler and all buttons that use it will immediately reflect any changes. If you do that, a script local variable can hold qTab and you won't need the global, but it depends on how your stack is set up.

And welcome to LiveCode. I'm with you, I learned by jumping in and making all kinds of mistakes. That was a long time ago. The learning, I mean. Not the mistakes, which I still manage to produce with some regularity.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Re: problems scoring selections in a form

Post by p0ntif » Wed Jul 02, 2014 9:36 pm

jacque wrote:You're doing fine, and your buttons are named correctly.
...
And welcome to LiveCode. I'm with you, I learned by jumping in and making all kinds of mistakes. That was a long time ago. The learning, I mean. Not the mistakes, which I still manage to produce with some regularity.

Thanks for your help! Your examples are a great help, and I did not realize that names could be 2 separate words "r1" vs "r 1". that's great!

I switched up the button code to the following:

Code: Select all

on mouseUp
   evaluate(the short name of me)
   add the last word of the short name of me to qTab
end mouseUp
But I would still like to explore the idea of creating a handler to do it on the card. That would be interesting. Can you point me to a resource?

Also, the card code is as follows:

Code: Select all

on preOpenCard
   global qNbr, qArray, qTab
   
   #section 1
   put 1 into qNbr
   #section 2
   put "" into fld "Question"
   put 0 into qTab
   
   #section 3
   put "Question number 1" into qArray["1"]["question"]
   --
   put "Question number 2" into qArray["2"]["question"]
   --
   put "Question number 3" into qArray["3"]["question"]
   --
   put "Question number 4" into qArray["4"]["question"]
   --   
   put "Question number 5" into qArray["5"]["question"]
   --
   put "Question number 6" into qArray["6"]["question"]
   --
   put "Question number 7" into qArray["7"]["question"]
   --
   put "Question number 8" into qArray["8"]["question"]
   --
   put "Question number 9" into qArray["9"]["question"]
end preOpenCard

On openCard
   nextQuestion
end openCard

command nextQuestion
   global qNbr, qArray
   put qArray[qNbr]["question"] into fld "Question"
end nextQuestion

command evaluate
   global qNbr, qArray, qTab
   if qNbr < 9 then
      add 1 to qNbr
      nextQuestion
      put qTab into fld "Score"
   else
      go to card "Last"
   end if
end evaluate

Can you let me know if there are better ways of doing what I am doing? Also, a way to return the score based on the 9 button pushes? -put qTab into fld "Score"- doesn't seem to do the trick. It remains zero. I imagine there are, and am doing this as a beginner hobbyist without any formal training, and would appreciate any advice or suggestions. Thanks again!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: problems scoring selections in a form

Post by jacque » Wed Jul 02, 2014 11:35 pm

qTab is always zero because the button scripts don't declare it as a global, so the handlers in the card script don't know it changed. You can fix the problem by adding a "global qTab" to each button handler, but that isn't a great way to do it. Using a single card script is better, and since you asked...

Remove all the button scripts, they need to be completely empty. You can then either use a single handler in the card or assign a behavior to each button. Here's a way to do it in the card:

Code: Select all

local qTab

on mouseUp
  if "button" is in the name of the target and \
        the short name of the target begins with "r" then
    add the last word of the short name of the target to qTab
  end if
end mouseUp
This does a couple of things. Instead of "me" we use "the target" to see what was clicked. We make sure the clicked object is a button, and that its name begins with an "r". If so, then we add a value to qTab. If you really want to be safe, see if you can figure out how to also check to see if the button style is a radio button; that way if you happen to name a non-radio button with an "r" it won't trigger this handler. There's an even better way to check by using custom properties, but that's for later.

Note the declaration of the "local qTab" outside of and above the handler. This is called a "script local variable". Declaring a variable outside of any handler in the script makes its value accessible by any handler in that script. It isn't global to all of LiveCode, only to this particular script. That means you don't need to declare qTab in any of your card handlers. Just put the declaration at the top of the script before any other handlers and all the card handlers can use it.

You can do the same thing with the array and number variables, if you aren't using them in any other scripts.

EDIT: instead of checking if the short name begins with "r" you could alternately use: if word 1 of the short name of the target = "r"
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: problems scoring selections in a form

Post by dunbarx » Thu Jul 03, 2014 12:11 am

You must listen to Jacque.

When you have a free moment, and as an aside, think about this construction:

Code: Select all

#section 3
repeat with y = 1 to 9
   put "Question number" && y into qArray[y]["question"]
end repeat
What do you think?

Craig

p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Re: problems scoring selections in a form

Post by p0ntif » Fri Jul 04, 2014 4:51 am

jacque wrote:qTab is always zero because the button scripts don't declare it as a global, so the handlers in the card script don't know it changed. You can fix the problem by adding a "global qTab" to each button handler, but that isn't a great way to do it. Using a single card script is better, and since you asked...
Ah of course!
Your sample is neat. But here's a question. I must still set qTab to zero before adding to it, right?
Note the declaration of the "local qTab" outside of and above the handler. This is called a "script local variable". Declaring a variable outside of any handler in the script makes its value accessible by any handler in that script. It isn't global to all of LiveCode, only to this particular script. That means you don't need to declare qTab in any of your card handlers. Just put the declaration at the top of the script before any other handlers and all the card handlers can use it.
Oh that's interesting. Now here's a question, I still need to declare it for each card that uses it right? Perhaps I am not understanding this completely, but when would you use a global variable instead of a local variable? It seems that I have just been using global variables.

p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Re: problems scoring selections in a form

Post by p0ntif » Fri Jul 04, 2014 4:56 am

dunbarx wrote:You must listen to Jacque.

When you have a free moment, and as an aside, think about this construction:

Code: Select all

#section 3
repeat with y = 1 to 9
   put "Question number" && y into qArray[y]["question"]
end repeat
What do you think?

Craig

I think I learn something new every day. The repeat function is a nice one. i need to try that.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: problems scoring selections in a form

Post by jacque » Fri Jul 04, 2014 8:09 pm

What we're talking about is called the "scope" of different types of variables. This is covered pretty well in the user guide, starting on page 141. You can open the guide from the Help menu or from the Resources icon in the toolbar. There are three levels of variable scope; those that apply only to the current handler, those that apply only to the current script, and those that apply globally everywhere. If you want a value to be available to any script anywhere, then you do need a global. If all handlers that use the value are contained in a single script, a script local is the best way. In general we try to avoid too many globals for various reasons, but there is no hard rule about it.

So in your button scripts (if you continue to use those) you do need to declare the global in each one. When you initialize the global to zero, it will be zero everywhere. You'll want to initialize it in whatever handler does the setup before the user begins answering questions. After that you can just add to it on each button press.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply