Page 1 of 2

building an answer string programmatically

Posted: Mon Jan 11, 2021 5:26 pm
by bbalmerTotalFluency
Hi:

I'd like to produce this code answer "how many" with "all" or "none" programmatically by building the options part as a text string. For the life of me I can't work out how to do it.

so I am looking to build a string that looks like "all" or "none", put it into x so I can write
answer "how many" with x

Is there a way to do this? My life, which is considerably like a doughnut would improve to be a veritable jam doughnut were there a solution.

Bruce

Re: building an answer string programmatically

Posted: Mon Jan 11, 2021 6:51 pm
by jmburnod
hi Bruce,
Hi,
You may use a group instead an answer dialog.
A group with a grc rectangle with the stack rect blendlevel 100.
This group must have a script to trap mouseevents.
best regards
Jean-Marc

Re: building an answer string programmatically

Posted: Mon Jan 11, 2021 6:54 pm
by dunbarx
Hmmm.

Welcome to LC and the forum.

Do you mean something like this:

Code: Select all

on mouseUp
    put "All" into firstChoice
    put "None" into secondChoice
    answer "How many" with firstChoice or secondChoice
  end mouseUp
Craig

Re: building an answer string programmatically

Posted: Mon Jan 11, 2021 6:55 pm
by dunbarx
Jean-Marc.

Either you or I are off in the weeds. Let me check my immediate surroundings. Hmmm. Leafy.

Craig

Re: building an answer string programmatically

Posted: Mon Jan 11, 2021 7:00 pm
by dunbarx
Bruce.

Or did you mean something like:

Code: Select all

on mouseUp
   put "All or None" into temp
   do "answer with" && temp
end mouseUp
which looks like it ought to work, but does not. But before I start fiddling with this, is that what you meant?

Craig

Re: building an answer string programmatically

Posted: Mon Jan 11, 2021 11:46 pm
by jmburnod
Hi Craig,
I think you're right for me :D
Best
Jean-Marc

Re: building an answer string programmatically

Posted: Tue Jan 12, 2021 3:26 am
by dunbarx
Jean Marc.

Likely the first time.

Anyway, I thought I was adept at forming "do" constructions, having done so here and there since 1987. But this one will not come through. Might it be something about trying to squeeze this into an "answer" command?

Craig

Re: building an answer string programmatically

Posted: Tue Jan 12, 2021 3:07 pm
by dunbarx
Jean-marc.

I just needed rest.

Bruce.

You can do this as follows:

Code: Select all

on mouseUp
   put "All or Nothing or Something" into temp
   do "answer test with" && temp
end mouseUp
The "do" construction evaluates the expression twice, so LC can build the final string for the answer command to understand. Just a note, if you use "none", LC will interpret that as a "0".

But you can fool LC if you separate that "None" from the string a bit, so during the first pass at evaluation, it becomes a literal, not a constant:

Code: Select all

on mouseUp
   put "All or" && quote & "None" & quote && "or Something" into temp
   do "answer test with" && temp
end mouseUp
Craig

Re: building an answer string programmatically

Posted: Tue Jan 12, 2021 9:06 pm
by jacque
dunbarx wrote:
Mon Jan 11, 2021 6:54 pm
Do you mean something like this:

Code: Select all

on mouseUp
    put "All" into firstChoice
    put "None" into secondChoice
    answer "How many" with firstChoice or secondChoice
  end mouseUp
That works fine for me, no "do" required.

Re: building an answer string programmatically

Posted: Tue Jan 12, 2021 9:52 pm
by dunbarx
Jacque.

But that does not quite do what the OP asked, it only places a few options into a few variables, instead of quoting each explicitly.

I think to deconstruct a SINGLE variable that contains several options requires that extra level of evaluation.

Craig

Re: building an answer string programmatically

Posted: Wed Jan 13, 2021 5:07 am
by bbalmerTotalFluency
Blimey chaps! Thank you.

A special thanks to Jean for his sneaky but brilliant solution. You just made my day better and since we are unlikely to meet, I will undertake to make two people's lives better today. Let us hope that exponential growth continues for some time.

Bruce

Re: building an answer string programmatically

Posted: Wed Jan 13, 2021 6:24 am
by bbalmerTotalFluency
Jean-Marc's "do" solution works brilliant for the example he gives: do "answer stuff with" && tOptions

But if I want to replace "stuff" with more the one word I receive the following error => execution error at line 141 (do: error in source expression)

Of course I could use underscores like this and it works "choose_one" but I fancy this is a bit ugly for non programmers.

Any ideas how I may include more than one word in Jean-Marc's otherwise 100% successful solution?

Bruce

Re: building an answer string programmatically

Posted: Wed Jan 13, 2021 9:03 am
by rkriesel
Hi, Bruce. Here's a way to invoke "answer" with multiple options, each with multiple words.

Code: Select all

command answerViaParams pPrompt, pOptions
   get "answer" & quote & pPrompt & quote && "with" && quote & item 1 of pOptions & quote
   repeat for each item tOption in item 2 to -1 of pOptions
      get it & " or " & quote & tOption & quote
   end repeat
   do it
   return it for value
end answerViaParams
Here's a demo.

Code: Select all

on mouseUp
   answerViaParams "please choose", "all,none,something else"
   breakpoint -- see "it" in the debugger's variables pane
end mouseUp
Does it give you what you need?

-- Dick

Re: building an answer string programmatically

Posted: Wed Jan 13, 2021 3:17 pm
by dunbarx
Jean-Marc is a wonderful coder. I am the "do" guy, though. Hmph.

I mention this because "do" constructions are considered old-fashioned, and Jean-Marc is anything but that.

Anyway, the "do" construction is still priceless, at least for me, because it forces another level of evaluation, and this produces a new string that LC can then understand. This was first elucidated by Danny Goodman, with HyperCard, back in the late 80's.

LC cannot resolve a string of "choices" directly, as presented in this thread. But after "do", er, does its work, it can. The new string is identical to one that actually separates and formats the several options, as if they were entered separately in the normal course of writing an "answer" command string.

Craig

Re: building an answer string programmatically

Posted: Wed Jan 13, 2021 11:50 pm
by jmburnod
Thank you for the flowers, but I'm a litle confused with this thread. I don't suggest using "do" in my post. :o
I choosed using a group way because it allows to have any LiveCode controls. I mean option menu, image, slider etc...
For example, i use this for preferences setting and to allows user to fill a field of a group "ask dialog with a screen keyboard.
Something i missed ?
Jean-Marc