Page 1 of 1

Assign multiple variables from a split list on the fly

Posted: Wed Oct 09, 2019 9:41 am
by MichaelBluejay
I can assign multiple new variables from a split list on the fly in Perl in one line of code:

Code: Select all

$data = 'Dave,46,sr. engineer,colorado,23';
($name, $age, $title, $city, $code) = split(',',$data);
In LiveCode, it takes multiple lines (the number of variables plus one):

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
I'd like a Perl-like way to assign multiple variables in one line of code, in keeping with LiveCode's marketing idea of "less code".

I know I can use arrays, but I'm not asking about arrays, I'm asking about regular variables.

Thank you.

[Edited to correct that LC arrays start at 1, not 0.]

Re: Assign multiple variables from a split list on the fly

Posted: Wed Oct 09, 2019 12:28 pm
by Klaus
Hi Michael,

Hint:
This is a place to discuss feature requests, the LC team does NOT read here on a regular base and does not take this discussion as "official". To place an "officical" feature request, you need to do it here: https://quality.livecode.com


Best

Klaus

Re: Assign multiple variables from a split list on the fly

Posted: Wed Oct 09, 2019 3:19 pm
by dunbarx
Michael.

Also, LC starts default keys with "1', not "0". There would be no "data[0]"

Know also that you can create your own keys explicitly:
put "dog" into myArray[pet]
put "chicken" into myArray[dinner]
put "cookie" into myArray[dessert]
In that way, of course, you could then have a "0" as your first key.

Craig

EDIT/ Michael has modified his original post, changing the (first) "0" key to "1". So the first line of this post is now moot.

Re: Assign multiple variables from a split list on the fly

Posted: Fri Oct 11, 2019 2:46 pm
by MichaelBluejay
Thank you, klaus, I didn't know that this was the wrong place to submit feature requests.

It would be a good idea if the sticky giving the details of this forum made that clear and provided the link to the proper place.

(Edit:) Actually, I just went to the link you provided, and it looks like that all I can do is submit Bug Reports, not Feature Requests.

Re: Assign multiple variables from a split list on the fly

Posted: Fri Oct 11, 2019 3:00 pm
by bogs
Looks can be deceiving, when you click that link, and then click "File a bug", you see this screen, which lets you choose bug or enhancement request
enhancementRequest.png
Drop it man!