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

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

dunbarx
VIP Livecode Opensource Backer
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?

Post by dunbarx » Thu Oct 10, 2019 1:45 pm

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

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

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

Post by MichaelBluejay » Thu Oct 10, 2019 3:09 pm

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.

FourthWorld
VIP Livecode Opensource Backer
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?

Post by FourthWorld » Thu Oct 10, 2019 4:54 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

[-hh]
VIP Livecode Opensource Backer
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?

Post by [-hh] » Thu Oct 10, 2019 7:14 pm

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

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

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

Post by bogs » Thu Oct 10, 2019 7:57 pm

Message noted, Richard.
Image

bwmilby
Posts: 438
Joined: Wed Jun 07, 2017 5:37 am
Location: Henrico, VA
Contact:

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

Post by bwmilby » Fri Oct 11, 2019 3:14 am

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.
Brian Milby

Script Tracker https://github.com/bwmilby/scriptTracker

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

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

Post by MichaelBluejay » Fri Oct 11, 2019 9:27 am

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.

MichaelBluejay
Posts: 222
Joined: Thu Jul 01, 2010 11:50 am

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

Post by MichaelBluejay » Fri Oct 11, 2019 9:36 am

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;

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”