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

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

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

Post by MichaelBluejay » Tue Oct 08, 2019 4:37 pm

Before I submit feature requests for these two items, I figured I should check to see if there's already a way to do these things that I don't know about.

Perl allows me to assign data to a bunch of new variables in one line. For example, from a string:

Code: Select all

$data = 'Dave,46,sr. engineer,colorado,23';
($name, $age, $title, $city, $code) = split(',',$data);
Or from an array:

Code: Select all

($var1,$var2,$var3) = @myArray;
Is there a way to do this in one line? I know I can do it in five, but I'm hoping for a Perl-like one-line solution.

Conversely, is there a way to take a bunch of variables and make a list out of them without having to manually put commas between them? e.g., rather than this:

Code: Select all

put variable1 &comma& variable2 &comma& variable3 &comma& variable4 &comma& variable5 into theList
(which is horrible for readability), it would be nice to have something like this:

Code: Select all

put variable1, variable2, varible3, variable4, variable5 into the items of theList

--(or)

put createList(variable1, variable2, varible3, variable4, variable5) into theList
Again, I know how I can do it in a few lines, I'm hoping for a one-line solution (in keeping with the LiveCode idea of "less code").

Updated to include solutions: Here
Last edited by MichaelBluejay on Fri Oct 11, 2019 9:35 am, edited 3 times in total.

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

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

Post by MichaelBluejay » Tue Oct 08, 2019 5:16 pm

Here's another one: a way to refer to all the parameters passed to a function.

In Perl, the function parameters are stored in the weirdly-named array $_. So, if I wanted to make a createList function so I could create lists in one line, I could do:

Code: Select all

print output($name, $age, $title, $city, $code);

sub output {
	for ($i = 0; $i < $#_; $i++) {
		$output .= $_[$i] . ','
	}
	chop($output);
	return $output;
}
But so far as I know, LC doesn't have a way to refer to all the parameter input. That makes coding a function for something like createList a hell of a lot trickier, especially when the number of parameters being passed could be different on different calls.

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 » Tue Oct 08, 2019 6:44 pm

You can use the "params" to delineate the parameters passed to either a function or command.

As for the one-liner. and I assume that this is cheating:

Code: Select all

put "X" into a ; put "Y" into b ; put "Z" into c
I cannot imagine how. Arrays are compact variable constructs, but the elements of which still must be populated explicitly. And ordinary variables as well need the same sort of care. This is just not part of how LC works.

But I am not sure I see the advantage, especially in terms of readability. True, something like:

Code: Select all

repeat with y = 1 to 10
  put y * 10 into myArray[y]
  end repeat
requires three lines, but I find it more than worth it to see how everything works. Could be I am stuck in xTalk heaven, though. :wink:

Craig

[-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] » Tue Oct 08, 2019 7:05 pm

Also read the dict about local, e.g.

Code: Select all

local a=1, b="two", c="http://livecode.com"
shiftLock happens

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

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

Post by MichaelBluejay » Tue Oct 08, 2019 7:18 pm

dunbar and -hh, what you said about variables doesn't seem to be related to the issues I posted about.

dunbar, thank you for sharing about the params, I was unfamiliar with that. Annoyingly, for a function "the params" returns in the form of: functionName("item1", "item2", "item3"), so I have to strip the function name and the opening and closing parenthesis before I can access the first or last item. This rectifies it:

put the params into input
delete char 1 to offset(quote,input)-1 of input
delete last char of input

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

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

Post by Klaus » Tue Oct 08, 2019 7:27 pm

MichaelBluejay wrote:
Tue Oct 08, 2019 7:18 pm
Annoyingly, for a function "the params" returns in the form of: functionName('item1', 'item2', 'item3'), so I have to strip the function name and the opening and closing parenthesis before I can access the first or last item.
No need for a verbose custom function, use the build-in -> paramcount
No need to strip anything, read PARAM and PARAMCOUNT entry in the dictionary, this is what you need.

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

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

Post by MichaelBluejay » Tue Oct 08, 2019 8:24 pm

If I want a list in the form of:

"item1", "item2", "item3", "item4", "item5", "item6"

and what I get from "the params" of my function is:

createList("item1", "item2", "item3", "item4", "item5", "item6")

then yes, I do have to strip the function name and the parentheses.

I didn't see anything in the dictionary for how to get only the list without the function name and parentheses.

In any event, armed with "the params", I can now get a list from a bunch of variables with one line, plus a very short function:

Code: Select all

put createList(variable1, variable2, variable3, variable4, variable5, variable6) into myList

function createList
   put the params into parameters
   delete char 1 to offset(quote,parameters)-1 of parameters
   delete last char of parameters
   return parameters
end createList
That is WAY better than cluttering up the code with a gazillion "&comma&" between each variable!

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

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

Post by Klaus » Tue Oct 08, 2019 8:53 pm

Yes, you are right, I had "the params" a bit different in mind, sorry. 8)

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 » Tue Oct 08, 2019 9:44 pm

The split and combine commands seem a natural fit for this problem, no?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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 » Tue Oct 08, 2019 10:35 pm

Richard.

Since the OP wanted to do it all in one line, I am wondering why this works (albeit in two lines):

Code: Select all

on mouseUp
   put "cat,dog,sheep" into temp
   split temp by comma
end mouseUp
but not this (one line):

Code: Select all

on mouseUp
      do "split" && quote & "cat,dog,sheep" & quote && "by comma"
end mouseUp
Craig

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 » Tue Oct 08, 2019 10:48 pm

dunbarx wrote:
Tue Oct 08, 2019 10:35 pm
Since the OP wanted to do it all in one line, I am wondering why this works (albeit in two lines):

Code: Select all

on mouseUp
   put "cat,dog,sheep" into temp
   split temp by comma
end mouseUp
but not this (one line):

Code: Select all

on mouseUp
      do "split" && quote & "cat,dog,sheep" & quote && "by comma"
end mouseUp
I use "do" only in those case where a more direct method is not provided.

In this case, once again we see that the simplicity of looking at the code once we get rid of "do" immediately clarifies the situation:

Code: Select all

split "cat,dog,sheep"
What is being split there?

The split command transforms a string variable into an array variable; combine reverses that.

Using an inline string constant prevents that transformation.

We needn't worry about the number of lines here. After all, the Perl example offered as desirable was six lines long. But more than the specific number of lines, the data going into the transformation will come from somewhere, and after the transformation will be used somewhere else. If putting a string constant into a variable adds a single line to the process, it's probably not adding much overhead to the process as a whole once it's in place in production code.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

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

Post by MichaelBluejay » Tue Oct 08, 2019 11:26 pm

My original post asked about two situations. None of the responses seem to be addressing them, they're addressing some other ideas. For example, I never said I was trying to wind up with an array.

The second problem is essentially solved, unless someone knows how to do it better. Rolling my own 4-line function is close enough, because then I can use a one-liner in my scripts.

As for the first problem, in Perl it's not six lines, it's one. That problem doesn't seem to lend itself to a solution by writing a custom function, because I'd need to declare a whole bunch of variables to be global or local, so it wouldn't work as a one-liner.

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 » Tue Oct 08, 2019 11:59 pm

MichaelBluejay wrote:
Tue Oct 08, 2019 11:26 pm
My original post asked about two situations. None of the responses seem to be addressing them, they're addressing some other ideas. For example, I never said I was trying to wind up with an array.
Understood, but since associative arrays are a natural fit for cases where you need to address a number of variables which can't be known in advance it seemed worth introducing it.
The second problem is essentially solved, unless someone knows how to do it better. Rolling my own 4-line function is close enough, because then I can use a one-liner in my scripts.
Glad you got it sorted.
As for the first problem, in Perl it's not six lines, it's one.
I was referring to your more recent example. But since this is such a small part of what I assume will be a complete and useful work, an extra line or two here and there isn't often the sort of thing I spend much time on.
That problem doesn't seem to lend itself to a solution by writing a custom function, because I'd need to declare a whole bunch of variables to be global or local, so it wouldn't work as a one-liner.
It could if you were willing to consider having the function return an array.

And since even the Perl example in your first post declares the variables, why would declaration not be acceptable in this language?
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] » Wed Oct 09, 2019 12:51 am

FourthWorld and dunbarx are right, this is a simple split/combine thing.
LC Script has no true lists as perl, so you have to use arrays.
If you wish to use true lists (and won't stay with perl) then you could use LC Builder.

For your original problem, you have in LiveCode (name,value)-pairs in a list (you may use any appropriate chars or even strings instead of "/" and ":"):

Code: Select all

local L="name:Dave/age:46/title:sr. engineer/city:colorado/code:23"
Put this into (key,element)-pairs using

Code: Select all

split L by "/" and ":" into A
Then you have A["age"], just as $age in perl, with value 46, etc.

To convert the array A back to the list L of (name,value)-pairs use

Code: Select all

put A into L; combine L with "/" and ":"
Because arrays in LC are associative you probably get back a different order of the pairs. If you need the order of the pairs then add a second value parameter and sort the combined list by that parameter.

So, three lines of LC-code. At least not more than your perl-code.
And moreover:
the keys of the array L is a cr-delimited list of the names only of the original list.

[By the way, you could try and read a bit more the dictionary. 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. And there is also param(i) in LC.]
Last edited by [-hh] on Wed Oct 09, 2019 1:31 am, edited 1 time in total.
shiftLock happens

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 » Wed Oct 09, 2019 12:51 am

Michael.

We are all volunteers here, with no direct connection to LiveCode the entity. We try to help new users in any way we can; we oftentimes make inside jokes and go off on tangents, sometimes unrelated tangents.

I hope that is going to be OK with you.

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”