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

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 7:54 pm


jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Can a function return two values?

Post by jiml » Thu Jul 01, 2021 8:42 pm

Chopping sentences by a word into 'subsentences'.

TheText to be parsed:
I am a Scottish man who lives in Bulgaria who eats cabbages. I see dead people who are freakin' everywhere! Who codes in Java? Do not ask for whom the bell tolls. It tolls for thee.

Code: Select all

put splitSentences(TheText, " who ") into theSplitTextArray
Note spaces around 'who'.

Code: Select all

function splitSentences pText, splitWord
   repeat with s = 1 to the number of sentences of ptext
      get sentence s of ptext
      split it by splitWord
      put it into splitText[s]
   end repeat
    return splitText
end splitSentences

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

Re: Can a function return two values?

Post by richmond62 » Thu Jul 01, 2021 8:49 pm

BUT . . .

does the splitWord include the splitWord in the derived 'subsentences?

BECAUSE if one uses itemDelimiter the itemDelimiter gets lost.

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

Re: Can a function return two values?

Post by dunbarx » Thu Jul 01, 2021 9:26 pm

BECAUSE if one uses itemDelimiter the itemDelimiter gets lost.
True. That makes wordOffset more straightforward. It simply identifies a word in the text string, and that value can be either the end or the beginning of each half.

Craig

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

Re: Can a function return two values?

Post by SparkOut » Thu Jul 01, 2021 9:54 pm

It's also true that if you already know the split word, it's pretty simple to

Code: Select all

return splitWord && splitText
(although you'd need to be a bit more sophisticated with a returned array, but hardly taxing)

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 » Fri Jul 02, 2021 1:12 am

Building a bit on Jim's suggestion, here's a more robust function.
I cheated a bit an included relative pronouns in the subordinating conjunctions list.

Code: Select all

constant kSubordinatingConjunctions = "after,although,as,as if, as long as,as much as,as soon as,as though,because,before,even if,even though,how,if,in as much as,in case,in order that,in order to,in so far as,insofar as,just as,lest,no matter,now that,once,provided,provided that,rather than,since,so that,than,that,though,till,unless,until,when,whenever,where,whereas,wherever,whether,which,whichever,while,who,whoever,whoever,whomever,whosever,why"
function splitSentences pText
   local tSplitText, tText
   
   set the itemdelimiter to comma
   replace "(" with space in pText
   replace ")" with space in pText
   repeat with s = 1 to the number of sentences of pText
      put sentence s of ptext into tText
      repeat for each item tSplitWord in kSubordinatingConjunctions
         if space&tSplitWord&space is in tText then
            replace space&tSplitWord&space with cr&tSplitWord&space in tText
         end if
      end repeat
      split tText by cr
      put tText into tSplitText[s]
   end repeat
   breakpoint
   return tSplitText
end splitSentences
Still will have some problems with constructs like "no matter who..." and "no matter what..."
and will split "as long as" twice (a second time for the "as") but it's another step along the way.

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 » Fri Jul 02, 2021 4:16 am

A revision does a better job of things.
Still will trip over some simple things like "there it is as you like it" and can't differentiate between subordinating conjunctions and adverbial clauses, but I'm not sure how much that can be automated away.

Code: Select all

constant kSubordinatingConjunctions = "why,whosever,whomever,whoever,whoever,who,while,whichever,which,whether,wherever,whereas,where,whenever,when,until,unless,till,though,that,than,so that,since,rather than,provided that,provided,once,now that,no matter,lest,just as,insofar as,in so far as,in order to,in order that,in case,in as much as,if,how,even though,even if,before,because,as though,as soon as,as much as,as long as,as if,as,although,after"
function splitSentences pText
   local tSplitText, tText
   
   set the itemdelimiter to comma
   replace "(" with space in pText
   replace ")" with space in pText
   replace comma with space in pText
   repeat with s = 1 to the number of sentences of pText
      put sentence s of ptext into tText
      repeat for each item tSplitWord in kSubordinatingConjunctions
         if space&tSplitWord&space is in tText then
            replace space&tSplitWord&space with cr&tSplitWord&numtochar(3) in tText
         end if
      end repeat
      replace numtochar(3) with space in tText
      split tText by cr
      put tText into tSplitText[s]
   end repeat
   return tSplitText
end splitSentences

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

Re: Can a function return two values?

Post by richmond62 » Fri Jul 02, 2021 9:19 am

can't differentiate between subordinating conjunctions and adverbial clauses
-
stew.jpg
-
https://youtu.be/sLeG7gxIJx4

SURELY the point of the exercise is to SPLIT a sequence of words in half somewhere and NOT get into
grammatical nit-picking. 8)

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Can a function return two values?

Post by jiml » Fri Jul 02, 2021 6:43 pm

MarkW wrote:
A revision does a better job of things.
Nice!

Post Reply

Return to “Talking LiveCode”