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

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

Post by MichaelBluejay » Wed Oct 09, 2019 10:31 pm

Update: There is indeed an easier way. See below.

Writing a function to create a tab-delimited list is even harder than I imagined, because LC doesn't properly count the number of parameters if any variable contains a comma!

Code: Select all

put "Oct. 2, 2019" into tDate
put "Becky" into tName

put createTabList(tDate,tName) into tList

function createTabList
  answer the number of items in the params -- returns 3, not 2
end createTabList
I can't test for the characters between the parameters (",") because the user might have entered that sequence of characters into an input field.

The only way I see around this is to specify receiving variables in the function handler, specifying more than I'd ever use.

Code: Select all

function createTabList v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
  put v1 & tab & v2 into output
  if v3 is not empty then put v3 & tab after output
  if v4 is not empty then put v4 & tab after output
  if v5 is not empty then put v5 & tab after output
  if v6 is not empty then put v6 & tab after output
  if v7 is not empty then put v7 & tab after output
  if v8 is not empty then put v8 & tab after output
  if v9 is not empty then put v9 & tab after output
  if v10 is not empty then put v10 & tab after output
  return output
end createTabList
Yes, I could use a repeat loop with "do" for fewer lines of code.
Last edited by MichaelBluejay on Thu Oct 10, 2019 9:37 am, edited 3 times in total.

[-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 10:32 pm

What's going on here?

Code: Select all

put (var1|var2|var3|var4|var5) into theList
is wrong syntax.
You can't use anything else but a comma for an enumeration of variables!

[Edit: Removed a note about using space as parameter separator in the definition of functions or handlers. I was wrong: This is allowed (but of course not in the call of the functions/handlers).]
Last edited by [-hh] on Thu Oct 10, 2019 5:04 am, edited 2 times in total.
shiftLock happens

[-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 10:50 pm

In sum, as we now know what you really want.

1. I gave you examples for several scenarios. The third example was the one you wanted.

Code: Select all

put "variable1,variable2,variable3" into theList --  the string of items 
put "variable1","variable2","variable3" into theList -- items of strings
put (variable1,variable2,variable3) into theList -- items of variable values
Glad you got it.

2. In the dictionary you can find "param".
Your perl routine can easily be translated to LC using param(i).

3. As you fear arrays here an easy way to do your first question. It is the usual approach of dunbarx for setting variables from name-strings (I learned it from him when I was beginner).

Code: Select all

on setVariables
  put getList() into tList
  repeat with i=1 to the num of lines of tList
    put line i of tList into tI
    do "put "&quote&item 2 of tI&quote&" into "& item 1 of tI
  end repeat
end setVariables

function getList
  return "tMBName,Dave" &cr& "tMBAge,46" &cr& \
        "tMBTitle,sr. engineer" &cr& "tMBCity,colorado" &cr& \
        "tMBCode,23"
end getList
If you wish to check the variables then write the following at the end of handler setVariables.
[Edit. Corrected: the variableNames is not buggy (what I originally wrote)]

Code: Select all

put line 2 of the variableNames into tV
 repeat for each item I in  tV
    if I begins with "tMB" then put cr & I &"="& value(I) after tS
 end repeat
put char 2 to -1 of tS
4. You wrote: Writing a function to create a tab-delimited list is even harder than I imagined.
I wrote: write theList separated with comma (any itemdelimiter). Then

Code: Select all

replace comma with tab in theList 
5. I wrote: Note. In your list each item begins with a space!
You wrote: No, it doesn't. Try it.

Of course it does (except the first item, sorry). You have a space after each comma.

LC is replacing a *leading* space for you when you use variables. In this case the space has no effect on the output. But replace the parentheses with quotes to get the input items then the leading space is not removed. That's what Klaus and other experienced users here say with "watch spaces after separators".

p.s. That you tell me to try a method I showed you is really funny...
Last edited by [-hh] on Wed Oct 09, 2019 11:48 pm, edited 1 time in total.
shiftLock happens

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 » Wed Oct 09, 2019 11:14 pm

Use paramcount() instead:

Code: Select all

function createTabList
  ## answer the number of items in the params 
  ## returns 3, not 2
  answer paramcount() 
  ## returns 2, correctamente
end createTabList

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 » Wed Oct 09, 2019 11:33 pm

MichaelBluejay wrote:
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.
Yes, I was also surprised at your recent interest in string concatenation here, given the very different original problem described as an interest in discrete variable names.

I had thought maybe you'd moved on to a different interest. Seems I was mistaken.

I know how to build your system easily, efficiently, and even reusably, but I must admit I don't grasp the benefit of the unique constraints imposed on this one corner of it. And since there's no disputing personal preferences, I'll just say I'm out of my depth on this one, and will leave you in the capable hands of our other forum members.

Carry on...
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 » Thu Oct 10, 2019 12:23 am

-hh wrote: I gave you examples for several scenarios.
Yes, you gave me two that were broken and one that works. Thank you for the one that works.
-hh wrote:function createTabList v1 v2 v3 v4 v5 v6 v7 v8 v9 v10is wrong syntax. You can't use anything else but a comma for an enumeration of parameters! A space doesn't separate parameters.
And yet the code I posted, with space delimiters, works perfectly. When I said it would be a good idea to test code before posting as a solution, I should have added, try code you think doesn't work before you say it doesn't work. This isn't the first time that my working code has been tagged as non-working.
-hh wrote:As you fear arrays...
Don't talk that way to me. I don't "fear" arrays. I use them. I'm using them in this project. But there are some scenarios in which I don't want to use them. Don't be such an cultist for LiveCode that you have to attack a fellow coder for not using your preferred method simply so you don't have to admit that another language can do something with less code.
-hh wrote:...here an easy way to do your first question...
It's neither easy nor works for its intended purpose. You've hard-coded the values in. The whole point of the Perl one-liner is to split up some string (which will not always have the same value!) and assign it to different unique variables on the fly. How are you not understanding this?
-hh wrote:I wrote: write theList separated with comma (any itemdelimiter). Then replace comma with tab in theList
Either you're not bothering to read my posts before you fire off your replies or else you're trolling me. I explained why that doesn't work, and I explained *why*.
-hh wrote:I wrote: Note. In your list each item begins with a space! You wrote: No, it doesn't. Try it. (-hh replies:) Of course it does (except the first item, sorry). You have a space after each comma.
(groan) THE SPACES AREN'T IN THE DATA! On p.2 you posted this code, as though I had spaces that needed to be removed:

Code: Select all

put (var1, var2, var3, var4, var5) into theList
replace comma&space with tab in theList
Here's the reality:

Code: Select all

put "apples" into var1
put "berries" into var2
put "cherries" into var3
put "dates" into var4
put "figs" into var5
put (var1, var2, var3, var4, var5) into theList
put theList -- returns "apples,berries,cherries,dates,figs", with no spaces
Last edited by MichaelBluejay on Thu Oct 10, 2019 12:51 am, edited 1 time 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 » Thu Oct 10, 2019 12:43 am

klaus, thank you for paramCount(). With that, and param(x) instead of the params:

Code: Select all

put createTabList(var1, var2, var3, var4, var5, var6) into tTabbedList

function createTabList
   repeat with prm = 1 to paramCount()
      put prm & tab after output
   end repeat
   delete last char of output
   return output
end createTabList

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 12:54 am

FourthWorld, I posted about various problems here. Let me summarize the problems and solutions.

(1) Split a string and assign it to variables on the fly in one line, a là Perl: ($name, $age, $title, $city, $code) = split(',',$data);

Solution: This is possible with a separate function, with some limitations:
Either the function must be hardcoded to accept a specific number of variables, or else the variables must be declared beforehand with "global" or "local".

(2) Create a comma-delimited list from a bunch of variables in one line, without cluttering the code with lots of "&comma&" words.

Solution: put (var1, var2, var3, var4, var5) into tList -- LC adds the commas

(3) Create a tab-delimited list from a bunch of variables in one line, without cluttering the code with lots of "&comma&" words.

Solution:

Code: Select all

put createTabList(var1, var2, var3, var4, var5, var6) into tTabbedList

function createTabList
   repeat with prm = 1 to paramCount()
      put prm & tab after output
   end repeat
   delete last char of output
   return output
end createTabList
Note that decimals on integers formatted to multiple decimal places don't survive the roundtrip to createTabList() and back. (Pass in 1 formatted as 1.00, get back 1.) There are various remedies:

(a) Convert the number to a string before passing in: putCreateList( var1, var2, format(myInteger), var4) into tList

(b) Reformat the input received back from the function.

(c) Set the numberFormat() in the function and then subtract zero from any integers passed it. This doesn't work if you ever want any of your integers to remain as integers.
Last edited by MichaelBluejay on Fri Oct 11, 2019 9:23 am, edited 4 times in total.

[-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 4:00 am

1. You wrote: "It's neither easy nor works for its intended purpose. You've hard-coded the values in. The whole point of the Perl one-liner is to split up some string (which will not always have the same value!) and assign it to different unique variables on the fly. How are you not understanding this?"
and: "Split a string and assign it to variables on the fly in one line, a là Perl: ($name, $age, $title, $city, $code) = split(',',$data);
Status: No solution. Could put the split string into arrays, but not into variables."


Hard coded values: Your two lines of perl code

Code: Select all

$data = 'Dave,46,sr. engineer,colorado,23';
($name, $age, $title, $city, $code) = split(',',$data);
have two hard coded lists as input and use split.
To make the strings variable (use 'on the fly') you have to write a function/repeat, just as in LC.

As you don't want to use split in LC you could (as essentially already written) do the same similarly simple as follows.
(Name your fields exactly as the variables (for simplicity) and add a field "tNew")

Code: Select all

local tN="tName,tAge,tTitle,tCity,tCode" -- names of flds and variables

on action
  repeat for each item I in tN
    do "put fld I into " & I --> assigns values to variables <--
  end repeat
  -- use the variables, example: output their values, avoiding comma
  repeat for each item I in tN
    put cr & "• " & I & " is " & value(I) after output
  end repeat
  put char 2 to -1 of output
  -- put tName --> use directly their names works also
end action

on mouseUp -- example usage
  if "tNew" is not among the items of tN
  then put ",tNew" after tN -- change tN after adding the fld "tNew"
  action
end mouseUp
10 lines of code (local and action) that solve both of your initial questions and also your additional delimiter problem.
And it has no hard coded input but tN. You simply have to add fields and increase tN (you could do that by script). It just works, everybody can test it.

2. You wrote: "I never tried to separate values with bar lines." That wasn't addressed to you.

3. The first two of my examples

Code: Select all

put "variable1,variable2,variable3" into theList --  the string of items 
put "variable1","variable2","variable3" into theList -- items of strings
put (variable1,variable2,variable3) -- items of variable values
are not broken (as you wrote), they simply serve as different examples of how to create a list.
They create a string containing literal (sub-)strings as items (the third a string containing variable values as items). What doesn't work with them for you?

4. Space delimiters for parameters:
I have to correct my answer, sorry. You are right: LC is accepting space as parameter separator in the definition of a handler or a function (of course not in the call of the handler/function).

===
By the way. I like perl and use it for some special jobs by LC's shell(). Did you ever try to do that?
Last edited by [-hh] on Thu Oct 10, 2019 7:46 am, edited 2 times 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 » Thu Oct 10, 2019 5:16 am

This is going south quickly. We do not want another Amthony Black fiasco, after all.

@Michael. Nobody but you has criticized anything. Two things:

1- Some of your posts do not show adequate context. That is why I did not know, until you later filled me in, that your list consisted of populated variables, and not strings. It was not obvious. That is how we began this journey, if you recall.

2- Please lighten up. I say that hoping your enthusiasm will continue to grow, and that we will all play the LC game alongside you. Did I mention that forum members are all volunteers? For me, responding here is a great excuse to stop working for just a bit, something I look forward to.

We only occasionally find a new user with your energy; Lets have fun...

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 9:12 am

Thank you, Craig. I was an early fan of HyperCard. In 1987 I drove three hours to see Bill Atkinson give a lecture on it. Some of my stacks have gone from HyperCard to SuperCard to Revolution to LiveCode.

As for not knowing that my variables were variables, in my first post I wrote, "s there a way to take a bunch of variables...", and I named them variable1, variable2, variable3, etc. I don't think I could have been much clearer than that.

As for -hh, I'm not going to bother reading his posts any more. If there were a mute feature on this forum, I would block him.

[-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 9:50 am

Michael, what is your problem?
I wasn't harder than your replies.
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 » Thu Oct 10, 2019 11:43 am

Ah, there *is* a block feature! -hh blocked.

[-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 11:51 am

Go ahead with the parts of my code that you use. Good luck. :-)
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 11:58 am

As I said in a previous post in another thread, I can understand frustration, but I really don't think that is your issue here. Hermann's posts went beyond answering your questions and examples, and even hinted at how to use perl from within Lc (using the shell) for things you seem more familiar with handling through another language.

As someone who also has communication issues through text, I can understand sometimes not connecting through forum posts, but of the threads you've started and answered replies of people who are trying to help YOU in, despite good advice coming back, your thorny, prickly, and pretty well rude.

Grow up a bit, Michael, and try responding with a better attitude.
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”