Sequential variable names
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Sequential variable names
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
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
Re: Sequential variable names
Hi Richmond,
you can only DO this!
But in cases like this ARRAYs are the thing to use:
Or do you fear arrays?
Oops, sorry I used that LANGUAGE!
Best
Klaus
you can only DO this!
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
...
Oops, sorry I used that LANGUAGE!
Best
Klaus
-
- Posts: 222
- Joined: Thu Jul 01, 2010 11:50 am
Re: Sequential variable names
Not funny.
Re: Sequential variable names
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.
AND it is aimed towards Richmond, who actually has a sense of humour, albeit a strange one.
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Sequential variable names
No, I don't fear arrays, and I don't fear Klaus's sense of humour either.
I only asked this question because it occurred to me in an odd dream in the middle of the night.
I only asked this question because it occurred to me in an odd dream in the middle of the night.
Re: Sequential variable names
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
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
Re: Sequential variable names
Do not tempt fate!
Code: Select all
...
put line x of field ("namesList" & tmpNum) into tmpName
...
Re: Sequential variable names
But, but.... awwwww shucky darns, if I don't tempt Fate, who will ?
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Sequential variable names
Dunno: I do it quite a lot.I don't tempt Fate
Re: Sequential variable names
Hey, that was just an ADVICE, but if you like errors like "Object not found" go ahead.
Re: Sequential variable names
I used to have an open mind, but it kept falling out. You mean like that?
Re: Sequential variable names
Really not sure anymore!
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Sequential variable names
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:
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.]
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.
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 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
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