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: Klaus, FourthWorld, heatherlaine, kevinmiller

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

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

Post by MichaelBluejay » Wed Oct 09, 2019 9:19 am

-hh wrote:So, three lines of LC-code. At least not more than your perl-code.
Except that it doesn't solve the problem I actually asked about. Like I said, I don't want an array, I want normal variables.
-hh wrote:By the way, you could try and read a bit more the dictionary.
(groan)
-hh wrote:For example
put "variable1,variable2,variable3" into theList or
put "variable1","variable2","variable3" into theList or
put (variable1,variable2,variable3) into theList (variable values)
just work. No createList() needed.
The first two do not work:

Code: Select all

put "apples" into fruit1
put "berries" into fruit2
put "cherries" into fruit3

-- Returns the literal, "fruit1, fruit2, fruit3", not the values:
put "fruit1, fruit2, fruit3" into theList 

-- Returns the literal, "fruit1, fruit2, fruit3", not the values:
put "fruit1","fruit2","fruit3" into theList

-- Returns "apples, berries,cherries":
put (fruit1, fruit2, fruit3) into theList
So, the third example does indeed work, and obviates the need for a separate function, thank you. And let's note that the parentheses are optional.

However, I'm pretty confident that that's nowhere to be found in the Dictionary, or any of the LiveCode documentation for that matter. I'm willing to stand corrected if someone can point out otherwise.

Returning to the first problem, here's how I can do it in Perl:

Code: Select all

$data = 'Dave,46,sr. engineer,colorado,23';
($name, $age, $title, $city, $code) = split(',',$data);
Here's what it takes in LiveCode:

Code: Select all

put 'Dave,46,sr. engineer,colorado,23' into data
split data by comma
put data[1] into theName
put data[2] into age
put data[3] into title
put data[4] into city
put data[5] into theCode
For a given string of data, that's six lines of code instead of one. And every extra variable increases the code length. Since no solutions have been posted, I'll proceed to submit this as a feature request.
Last edited by MichaelBluejay on Wed Oct 09, 2019 12:40 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6833
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 » Wed Oct 09, 2019 11:51 am

MichaelBluejay wrote:
Wed Oct 09, 2019 9:19 am
Here's what it takes in LiveCode:

Code: Select all

put 'Dave,46,sr. engineer,colorado,23' into data
split data by comma
put data[0] into theName
put data[1] into age
put data[2] into title
put data[3] into city
put data[4] into theCode
For a given string of data, that's six lines of code instead of one.
What is happening in the code that makes having brackets as part of the variable name prohibitive?

The method suggested in this thread would yield a variable named t["theName"], which is indeed longer than "theName" but only by a few characters, and keeps all of the related data in a single structure, which can be useful for passing around, serializing, etc.

Depending on usage there may be other benefits as well. For example, if you were putting the data into fields the loop can become a little simpler with an array:

Code: Select all

on PopulateForm pDataA
   repeat with i =  1 to the number of flds
      put pDataA[the short name of fld i] into fld i
   end repeat
end PopulateForm
If we hand-coded individual var names we'd have a function that could only be used in one case. But here we have a function that can be used with any array of any size in any layout where we have a set of fields with corresponding names.

Depending on the full scope of your app's functionality we may be able to find other opportunities to generalize code for reuse.

And every extra variable increases the code length
An array is a single variable. ;)

As for code length, over the course of a production work how will this difference affect the deliverable?

Since no solutions have been posted, I'll proceed to submit this as a feature request.
I like it. And as a function it may lend itself well to finding someone in the community with a similar need and C++ skills to implement it. Please post the URL when it's submitted so we can follow along. Thank in advance for submitting that.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

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

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

Post by Klaus » Wed Oct 09, 2019 12:15 pm

Very important hint:

Code: Select all

...
put 'Dave,46,sr. engineer,colorado,23' into data
split data by comma
## put data[0] into theName
put data[1] into theName
put data[2] into age
...
In LC the keys of an Array start at 1 (ONE)!

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

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

Post by MichaelBluejay » Wed Oct 09, 2019 12:46 pm

FourthWorld wrote:What is happening in the code that makes having brackets as part of the variable name prohibitive?
For the umpteenth time, I DON'T ALWAYS WANT TO USE ARRAYS. It's my personal preference. End of story. If you want to use arrays, great, do it. I don't.

Klaus, thank you for the reminder about 1-based arrays. I edited my post so that I don't accidentally mislead others (though I can't edit the posts of those who quoted me).

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6833
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 » Wed Oct 09, 2019 1:51 pm

MichaelBluejay wrote:
Wed Oct 09, 2019 12:46 pm
FourthWorld wrote:What is happening in the code that makes having brackets as part of the variable name prohibitive?
For the umpteenth time, I DON'T ALWAYS WANT TO USE ARRAYS. It's my personal preference. End of story.
Fair enough. My aim is merely to provide solutions to problems presented in these forums. There's no disputing personal preference, but maybe others with a similar need will find the examples here useful.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

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

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

Post by jacque » Wed Oct 09, 2019 6:28 pm

MichaelBluejay wrote:
Wed Oct 09, 2019 9:19 am
So, the third example does indeed work, and obviates the need for a separate function, thank you. And let's note that the parentheses are optional.
LC interprets anything in quotes as a string literal, which is why the first two don't return what is expected. Without quotes, LC interprets the string as a variable, at least at first.

That's why all literals should be in quotes (such as control names.) If quotes are omitted, LC will try to use the word as a variable and if one isn't found it then looks for an object with that name. You can avoid the double lookup by always quoting literals. You'll also avoid issues where a named control may be the same as a variable (which isn't usually a great idea but you can do it.)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

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

Post by MichaelBluejay » Wed Oct 09, 2019 6:38 pm

Riddle me this:

Thanks to -hh, we know that we can create a comma-separated list without having to clutter up the code with lots of "& comma &" phrases by doing:

put (var1, var2, var3, var4, var5) into theList

But let's say I need a tab-delimited list (because that's what the Data Grid uses), and that some of the variable values could contain commas. It seems then there's no easy way to do it, since I can't replace commas with tabs, or split the items by commas. Seems like I'm back to creating a custom function (e.g., createTabList(var1, var2, var3...), if I want to avoid lots of "& tab &" phrases in my code, right?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6833
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 » Wed Oct 09, 2019 7:04 pm

The format function can help with that.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2120
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] » Wed Oct 09, 2019 7:20 pm

Code: Select all

put (var1, var2, var3, var4, var5) into theList
replace comma&space with tab in theList
Note. In your list each item begins with a space!
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6160
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 » Wed Oct 09, 2019 7:38 pm

Michael.

You are doing great.

LC is flexible in its syntax, more so, as I said, than I would oftentimes like. So just to nitPick:

Code: Select all

put (var1, var2, var3, var4, var5) into theList
really should be:

Code: Select all

put "var1, var2, var3, var4, var5" into theList
You will soon see that these things matter, especially when trying to read the code of others. The conventions really help. These include variable naming and other things.

Like "nitPick". See?

Craig

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

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

Post by Klaus » Wed Oct 09, 2019 7:45 pm

Hi Craig,

nope, the first creates a list from the CONTENT of the five variables!
The second will create one just the string -> var1, var2, var3, var4, var5

Here a little test, make your own conclusions :-)

Code: Select all

on mouseUp
   put "eins" into var1
   put "zwei" into var2
   put "drei" into var3
   put (var1, var2, var3) into theList
   put theList
   ## -> eins,zwei,drei
end mouseUp
Please note that LC even takes care of the SPACES inside the PUT command!


Best

Klaus

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

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

Post by MichaelBluejay » Wed Oct 09, 2019 9:29 pm

It might be a good idea to test suggested answers before posting them.
-hh wrote:Note. In your list each item begins with a space!
No, it doesn't. Try it.
dunbarx wrote:You will soon see that these things matter...
Indeed, inasmuch as the code you offered doesn't work and the code you criticized does.

FourthWorld, I looked up the format function, but I don't see how it's relevant.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6833
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 » Wed Oct 09, 2019 9:48 pm

MichaelBluejay wrote:
Wed Oct 09, 2019 9:29 pm
FourthWorld, I looked up the format function, but I don't see how it's relevant.
With concatenation:

Code: Select all

   put "Dave" &tab& "46" &tab& "sr. engineer" &tab& "colorado" &tab& "23" into tVar
With the format function:

Code: Select all

   put format("Dave\t46\tsr. engineer\tcolorado\t23") into tVar
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

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

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

Post by MichaelBluejay » Wed Oct 09, 2019 9:58 pm

I still don't see how that's relevant. As I posted, I want to created a list of a bunch of tab-delimited variables, not put a string containing tabs into a variable.

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

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

Post by jacque » Wed Oct 09, 2019 10:26 pm

MichaelBluejay wrote:
Wed Oct 09, 2019 6:38 pm
But let's say I need a tab-delimited list (because that's what the Data Grid uses), and that some of the variable values could contain commas. It seems then there's no easy way to do it, since I can't replace commas with tabs, or split the items by commas. Seems like I'm back to creating a custom function (e.g., createTabList(var1, var2, var3...), if I want to avoid lots of "& tab &" phrases in my code, right?
If a variable contains commas then you need to change the list to use a different delimiter. You can set a custom delimiter to anything that you know isn't in the set of items you want to parse. I'll use a pipe here but it can be anything, including multiple characters; you could use "zzzzz" as a delimiter if you wanted to.

Code: Select all

put (var1|var2|var3|var4|var5) into theList
set the itemDelimiter to "|"
repeat for each item i in theList
 put i & tab after tNewList
end repeat
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”