Assign multiple variables from a split list on the fly

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

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

Assign multiple variables from a split list on the fly

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

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.]
Last edited by MichaelBluejay on Fri Oct 11, 2019 2:45 pm, edited 1 time in total.

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

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

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

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6160
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

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

Post by dunbarx » Wed Oct 09, 2019 3:19 pm

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.
Last edited by dunbarx on Fri Oct 11, 2019 3:57 pm, edited 3 times in total.

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

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

Post by MichaelBluejay » Fri Oct 11, 2019 2:46 pm

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.

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

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

Post by bogs » Fri Oct 11, 2019 3:00 pm

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!
Image

Post Reply

Return to “Feature Requests”