Can a function return two values?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Can a function return two values?

Post by SparkOut » Thu Jan 31, 2019 9:12 pm

At a tangent, I was wondering where I could find an (old) article by (IIRC) Richard (Fourth World) concerning a technique to return a value from a function as well as specific value in "the result". I am at a loss to track it down.

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Can a function return two values?

Post by Klaus » Thu Jan 31, 2019 9:15 pm


SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Can a function return two values?

Post by SparkOut » Thu Jan 31, 2019 9:27 pm

Well, yes. But not successful in tracking it down. I am not even certain, but I think Richard was the author.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Can a function return two values?

Post by bogs » Thu Jan 31, 2019 9:45 pm

Did you check in the journal? If it isn't there, you might find it in his revNet plugin...
Selection_001.png
GoRev(Livecode)Net plugin...
or in the number of tutorials he had in a much older version of RR...
Selection_002.png
Transcript WWWWhhhhaaa....?
or possibly in this stack...
Selection_003.png
Meta-fan-tabulous!
or my last guess, the scripting conferences?
Image

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Can a function return two values?

Post by FourthWorld » Thu Jan 31, 2019 10:46 pm

SparkOut wrote:
Thu Jan 31, 2019 9:12 pm
At a tangent, I was wondering where I could find an (old) article by (IIRC) Richard (Fourth World) concerning a technique to return a value from a function as well as specific value in "the result". I am at a loss to track it down.
That may have been an either/or, as in being able to return a value from a command with "the result". If I'd written that a function can do both I don't recall what it is. And if I did, since apparently it's not easy to remember I don't think I'd recommend it even if I knew what it was.

An array seems simplest for the OP's use case.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Can a function return two values?

Post by dunbarx » Thu Jan 31, 2019 11:19 pm

You may be referring to the ability of a command handler, as opposed to a function handler, to return a result.

The difference is that the control structure "return" puts a value into "the result" whereas in a function handler, return, er, returns a value.

Code: Select all

on mouseUp
   resultTest
   answer the result
end mouseUp

on resultTest
   repeat with y = 1 to 10
      if y = 5 then return random(99)
   end repeat
end resultTest
Craig Newman
Last edited by dunbarx on Fri Feb 01, 2019 9:11 pm, edited 1 time in total.

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

Re: Can a function return two values?

Post by jacque » Fri Feb 01, 2019 6:32 pm

mwieder wrote:
Thu Jan 31, 2019 5:05 pm
I regularly use numtochar(3) as a delimiter when I need something that is guaranteed to be unprintable.
Me too, and also numtochar(8) as a secondary delimiter. No one can put those into text when typing.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Can a function return two values?

Post by richmond62 » Wed Jun 30, 2021 9:50 am

I am a simple soul (well, let's pretend that is true for the moment) . . .

Imagine a sentence:

"I am a Scottish man who lives in Bulgaria"

and imagine I want to chop this up into 2 subsentences:

"I am a Scottish man" and

"who lives in Bulgaria"

I have a feeling that a function to do that sort of thing should be about 5 to 10 minutes work:
-
SShot 2021-06-30 at 11.47.56.png
-

Code: Select all

on mouseUp
   put empty into fld "f1"
   put empty into fld "f2"
   put fld "ff" into FFFF
   put "z" into XXX
   repeat until FFFF is empty
      if (word 1 of FFFF) is not "who" and XXX is not "x" then
         put ((word 1 of FFFF) & " ") after fld "f1"
         delete word 1 of FFFF
      else
         put "x" into XXX
         put ((word 1 of FFFF) & " ") after fld "f2"
         delete word 1 of FFFF
      end if
   end repeat
end mouseUp
Of course that is extremely basic and just looks for the word "who", but looking for a designated item delimiter
would be just as easy.
Attachments
Splitter.livecode.zip
Here's the stack.
(1.14 KiB) Downloaded 117 times

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

Re: Can a function return two values?

Post by jacque » Wed Jun 30, 2021 5:52 pm

You're looking at writing a full syntax parser, which isn't an easy task. There are commercial programs that claim to do it but I'm not sure even those are perfect.

Google Docs tries to help with that if you turn on the option and it gets it wrong half the time.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Can a function return two values?

Post by mwieder » Wed Jun 30, 2021 6:13 pm

Well, leaving aside the fact that "who lives in Bulgaria" is a dependent clause rather than a subsentence (unless you want to stack a ? at the end, and then this becomes a different discussion)...

You might try breaking on a finite set of words that trigger a dependency, "who" being one of them.
But I think Jacque is right - trying to treat the English language as something logical is a fool's errand.

Jacque - if Google docs gets it right half the time, maybe you could just use half the results. :P

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

Re: Can a function return two values?

Post by jacque » Wed Jun 30, 2021 7:35 pm

mwieder wrote:
Wed Jun 30, 2021 6:13 pm
Jacque - if Google docs gets it right half the time, maybe you could just use half the results. :P
Heh. :) I wonder what it would do if I started writing like Yoda.
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: Can a function return two values?

Post by dunbarx » Wed Jun 30, 2021 7:43 pm

Richmond.

I may be either oversimplifying or trivializing what you want to do, but with your sentence in a field 1:
I am a Scottish man who lives in Bulgaria
and knowing where the break is going to be, either at a word, the word number, whatever:

Code: Select all

on mouseUp
   put fld 1 into tText 
   answer "Break where?" with "word" or "word number"
   if it is "word number" then
      ask "word number?"
      answer word 1 to it - 1 of tText & return & word it to 100000 of tText
   else if it is "word" then
      ask "Word?"
      put wordOffset(it,tText) into wordNum
      answer word 1 to wordNum - 1 of tText & return & word wordNum to 100000 of tText
   end if
end mouseUp
You could use the itemDel similarly.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Can a function return two values?

Post by richmond62 » Wed Jun 30, 2021 7:48 pm

You could use the itemDel similarly.
Indeed I could . . . but my 'problem' is that I spend an awful lot of time with children between 6 and 11 years old
so I tend to simplify things. 8)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Can a function return two values?

Post by richmond62 » Wed Jun 30, 2021 7:49 pm

Well, leaving aside the fact that "who lives in Bulgaria" is a dependent clause
Ooh, sorry, I forgot to wear my Grammar-Queen frock today.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Can a function return two values?

Post by richmond62 » Wed Jun 30, 2021 7:50 pm

a full syntax parser
About as likely as my waking up in bed one day and finding my wife has been replaced by a robot and I haven't noticed. 8)

Post Reply

Return to “Talking LiveCode”