Possible to easily assign lists to variables & vice-versa?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Possible to easily assign lists to variables & vice-versa?
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
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
-
- Posts: 222
- Joined: Thu Jul 01, 2010 11:50 am
Re: Possible to easily assign lists to variables & vice-versa?
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.
-
- VIP Livecode Opensource Backer
- Posts: 9823
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Possible to easily assign lists to variables & vice-versa?
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.
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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Possible to easily assign lists to variables & vice-versa?
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.
shiftLock happens
Re: Possible to easily assign lists to variables & vice-versa?
Message noted, Richard.
Re: Possible to easily assign lists to variables & vice-versa?
Back to the original question:
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:
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.
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
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
Brian Milby
Script Tracker https://github.com/bwmilby/scriptTracker
Script Tracker https://github.com/bwmilby/scriptTracker
-
- Posts: 222
- Joined: Thu Jul 01, 2010 11:50 am
Re: Possible to easily assign lists to variables & vice-versa?
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.
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.
-
- Posts: 222
- Joined: Thu Jul 01, 2010 11:50 am
Re: Possible to easily assign lists to variables & vice-versa?
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;