building an answer string programmatically

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

bbalmerTotalFluency
Posts: 52
Joined: Mon Apr 06, 2020 1:19 pm
Location: Thailand
Contact:

building an answer string programmatically

Post by bbalmerTotalFluency » Mon Jan 11, 2021 5:26 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: building an answer string programmatically

Post by jmburnod » Mon Jan 11, 2021 6:51 pm

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
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: building an answer string programmatically

Post by dunbarx » Mon Jan 11, 2021 6:54 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: building an answer string programmatically

Post by dunbarx » Mon Jan 11, 2021 6:55 pm

Jean-Marc.

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

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: building an answer string programmatically

Post by dunbarx » Mon Jan 11, 2021 7:00 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: building an answer string programmatically

Post by jmburnod » Mon Jan 11, 2021 11:46 pm

Hi Craig,
I think you're right for me :D
Best
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: building an answer string programmatically

Post by dunbarx » Tue Jan 12, 2021 3:26 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: building an answer string programmatically

Post by dunbarx » Tue Jan 12, 2021 3:07 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: building an answer string programmatically

Post by jacque » Tue Jan 12, 2021 9:06 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: building an answer string programmatically

Post by dunbarx » Tue Jan 12, 2021 9:52 pm

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

bbalmerTotalFluency
Posts: 52
Joined: Mon Apr 06, 2020 1:19 pm
Location: Thailand
Contact:

Re: building an answer string programmatically

Post by bbalmerTotalFluency » Wed Jan 13, 2021 5:07 am

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

bbalmerTotalFluency
Posts: 52
Joined: Mon Apr 06, 2020 1:19 pm
Location: Thailand
Contact:

Re: building an answer string programmatically

Post by bbalmerTotalFluency » Wed Jan 13, 2021 6:24 am

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

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: building an answer string programmatically

Post by rkriesel » Wed Jan 13, 2021 9:03 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: building an answer string programmatically

Post by dunbarx » Wed Jan 13, 2021 3:17 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: building an answer string programmatically

Post by jmburnod » Wed Jan 13, 2021 11:50 pm

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
https://alternatic.ch

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”