Page 4 of 4

Re: Possible to easily assign lists to variables & vice-versa?

Posted: Thu Oct 10, 2019 1:45 pm
by dunbarx
Michael,

Hermann is nothing if not forthright. He has no agenda other than trying to help. You have misinterpreted all he has tried to do, and the simple intentions behind it.

Lets all start from scratch. LC stuff only; what's next?

Craig

Re: Possible to easily assign lists to variables & vice-versa?

Posted: Thu Oct 10, 2019 3:09 pm
by MichaelBluejay
bogs, try looking in the mirror. I'm not the one telling others to "grow up a bit", which is quite condescending. And I don't use language like "as you fear arrays" like -hh did. And as for his supposed help, about half of it was irrelevant to what I was actually asking about, and most of the rest was just wrong.

Re: Possible to easily assign lists to variables & vice-versa?

Posted: Thu Oct 10, 2019 4:54 pm
by FourthWorld
FWIW phrases like "grow up" and presumptions of fear over things already clearly identified as mere personal preference do not reflect well in our community.

We strive to create a welcoming environment for everyone earnestly interested in getting the most out of LiveCode.

Forum posts are rarely edited or removed by moderators, making it the choice of the writer what they want to leave here as a legacy to be seen by thousands of newcomers.

Let's try to keep the tone of our conversations here at least in par with what we'd bring to a business luncheon with colleagues.

Re: Possible to easily assign lists to variables & vice-versa?

Posted: Thu Oct 10, 2019 7:14 pm
by [-hh]
MichaelBluejay wrote:And I don't use language like "as you fear arrays" like -hh did. And as for his supposed help, about half of it was irrelevant to what I was actually asking about, and most of the rest was just wrong.

The wonderful thing here is that everybody can take a piece of code and test for himself what's wrong or not ... :-)
And I'm very glad to hear that you don't fear arrays. That makes this thread obsolete.

Re: Possible to easily assign lists to variables & vice-versa?

Posted: Thu Oct 10, 2019 7:57 pm
by bogs
Message noted, Richard.

Re: Possible to easily assign lists to variables & vice-versa?

Posted: Fri Oct 11, 2019 3:14 am
by bwmilby
Back to the original question:

Code: Select all

on mouseUp
   local tData, tName, tAge, tTitle, tCity, tCode
   put "Dave,46,sr. engineer,colorado,23" into tData
   
   _split5 tData, tName, tAge, tTitle, tCity, tCode
   
   answer (tName, tAge, tTitle, tCity, tCode)
end mouseUp

private command _split5 pData, @pV1, @pV2, @pV3, @pV4, @pV5
   local tIndex, tItem
   put 0 into tIndex
   repeat for each item tItem in pData
      add 1 to tIndex
      do "put tItem into pV" & tIndex
   end repeat
end _split5
In your code, this does end up being a single line. The disadvantage is that you need to define the command for the number of parameters you want back. Using pass by reference, you can assign to the variables inside the command. Using do will be slower than writing it out the long way with 5 individual put statements.

If you are willing to use global variables or script local variables there is another option:

Code: Select all

local sName, sAge, sTitle, sCity, sCode

on mouseUp
   local tData
   put "Dave,46,sr. engineer,colorado,23" into tData
   
   _split tData, "sName", "sAge", "sTitle", "sCity", "sCode"
   
   answer (sName, sAge, sTitle, sCity, sCode)
end mouseUp

private command _split pData
   local tIndex, tItem
   put 1 into tIndex
   repeat for each item tItem in pData
      add 1 to tIndex
      do "put tItem into " & param(tIndex)
   end repeat
end _split
The advantage here is that the number of parameters does not need to be known in advance and can vary. In production some bounds checking would be good, but this is the general idea.

Re: Possible to easily assign lists to variables & vice-versa?

Posted: Fri Oct 11, 2019 9:27 am
by MichaelBluejay
Thank you, bwmilby, that's clever. I added this to the "solutions" post.

I didn't know about the @ notation, which I just looked up. That's going to simplify the code in my project because now there's less effort in making sure a handler can get a modified value back to the calling handler. People here are quick to point questioners to the LC dictionary, but that works only if you already know what to search for. There's no way I would have known to search for the @ sign.

Re: Possible to easily assign lists to variables & vice-versa?

Posted: Fri Oct 11, 2019 9:36 am
by MichaelBluejay
I forgot to mention in my original post (just updated it), that Perl's way of assigning variables on the fly works for arrays as well as split strings. I use it more frequently for arrays:

Code: Select all

($var1,$var2,$var3) = @myArray;