Sequential variable names

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

Post Reply
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Sequential variable names

Post by richmond62 » Thu Oct 10, 2019 5:15 pm

Let's suppose I am importing lots and lots of numbers into a stack and I want to assign each one a name,
but each variable has to have a name from a sequence; e.g. XX1, XX2, XX3 and so on.

Now I have no idea how many data items I will be importing.

I don't think I can do this sort of thing (pseudocode):

put 1 into Vname
repeat until dataStream is empty
put dataStream Item Vname into (XX & Vname)
add 1 to Vname
end repeat

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

Re: Sequential variable names

Post by Klaus » Thu Oct 10, 2019 5:19 pm

Hi Richmond,

you can only DO this! :D
But in cases like this ARRAYs are the thing to use:

Code: Select all

...
put 1 into Vname
repeat until dataStream is empty
  put Item Vname of dataStream into StreamArray[vName]
  add 1 to Vname
end repeat
...
Or do you fear arrays?
Oops, sorry I used that LANGUAGE! :D


Best

Klaus

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

Re: Sequential variable names

Post by MichaelBluejay » Thu Oct 10, 2019 5:26 pm

Not funny.

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

Re: Sequential variable names

Post by Klaus » Thu Oct 10, 2019 5:32 pm

Au contraire, mon ami, this is even ridiculous!
AND it is aimed towards Richmond, who actually has a sense of humour, albeit a strange one. :D

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Sequential variable names

Post by richmond62 » Thu Oct 10, 2019 6:53 pm

No, I don't fear arrays, and I don't fear Klaus's sense of humour either. :D

I only asked this question because it occurred to me in an odd dream in the middle of the night. 8)

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

Re: Sequential variable names

Post by bogs » Thu Oct 10, 2019 7:52 pm

I'll tell you what is really frightening, I've actually used the code Richmond posted to great effect, based off a self populating field (kinda).

Whenever another field was added or subtracted to a form, the fields name was added to the self populating field with 'cr & fieldName'. All the fields were named 'txtFld#" where # equaled 1, 2, 3... etc.

This was when I was very new to Lc, so the repeat loop I used differed from Richmond's example only in the repeat line and the variable name, mine was
repeat with x=1 to the number of lines of field "namesList"
put line x of field "namesList" & tmpNum into tmpName
add 1 to tmpNum
end repeat

It worked like a charm for me :D
Image

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

Re: Sequential variable names

Post by Klaus » Thu Oct 10, 2019 7:57 pm

Do not tempt fate! 8)

Code: Select all

...
put line x of field ("namesList" & tmpNum) into tmpName
...

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

Re: Sequential variable names

Post by bogs » Thu Oct 10, 2019 8:00 pm

But, but.... awwwww shucky darns, if I don't tempt Fate, who will ? :shock:
Image

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9388
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Sequential variable names

Post by richmond62 » Thu Oct 10, 2019 8:00 pm

I don't tempt Fate
Dunno: I do it quite a lot. 8)

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

Re: Sequential variable names

Post by Klaus » Thu Oct 10, 2019 8:13 pm

Hey, that was just an ADVICE, but if you like errors like "Object not found" go ahead. :D

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

Re: Sequential variable names

Post by bogs » Thu Oct 10, 2019 8:24 pm

I used to have an open mind, but it kept falling out. You mean like that? :twisted:
Image

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

Re: Sequential variable names

Post by Klaus » Thu Oct 10, 2019 8:27 pm

:shock: :shock: :shock: Really not sure anymore!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Sequential variable names

Post by [-hh] » Thu Oct 10, 2019 9:55 pm

Let me collect the methods because this is often asked in the forum.

We check the results of each of the following approaches with "the variableNames".
You can see there which variables exist after creation attempts.

This method produces repeatedly ONE variable tmpName, NOT what you want:

Code: Select all

on mouseUp 
  repeat with i= 1 to 200
    put comma & "hello #" &i after dataStream
  end repeat
  delete char 1 of dataStream; put "x" into XX
  repeat with i=1 to the num of items of dataStream
    put (XX & i) into tmpName -- tmpName has content (XX & i)
    put item i of dataStream into tmpName -- replaces the content of tmpName
  end repeat
  put the variableNames into s; replace comma with cr in s; put s -- check creation
  answer x42 -- answers "x42", because x42 is not intialised as variable
end mouseUp
Using Do.
Using do you can have what you want.
The variables exist until the end of the handler where you define them.
[If the variable content contains quote or cr then put it into a variable before using do.]

Code: Select all

on mouseUp -- creates variables x1, x2, ..., x200
  repeat with i= 1 to 200
    put comma & "hello #" &i after dataStream
  end repeat
  delete char 1 of dataStream; put "x" into XX
  repeat with i=1 to the num of items of dataStream
    put (XX & i) into tmpName -- tmpName has content (XX & i)
    do "put item i of dataStream into " & tmpName -- new variable (XX & i)
  end repeat
  put the variableNames into s; replace comma with cr in s; put s -- check creation
  answer x42 -- answers "hello #42"
end mouseUp
Using Array.
If you like arrays, at least are not afraid of them, the method of Klaus reads in this scenario as follows.
It has moreover the advantage that you can optionally define sArray as a script local variable and thus use it in the current script also outside of the current handler.

Code: Select all

local sA -- the variables array
on mouseUp -- creates sA["x1"], sA["x2"], ... , sA["x200"]
  repeat with i= 1 to 200
    put comma&"hello #" &i after dataStream
  end repeat
  delete char 1 of dataStream; put "x" into XX
  put empty into sArray
  repeat with i=1 to the num of items of dataStream
    put item i of dataStream into sA[XX & i] -- new element with key (XX & i)
  end repeat
  put the variableNames into s; replace comma with cr in s; put s -- check creation
  answer sA["x42"] -- answers "hello #42"
end mouseUp
shiftLock happens

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”