Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Naming a variable based off the value of another variable. (Horrific)
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ModsCentral




PostPosted: Tue Mar 27, 2012 12:05 pm   Post subject: Naming a variable based off the value of another variable. (Horrific)

What is it you are trying to achieve?
I am trying to create a module of which can be distributed. Now this module is trying to achieve what is being down in current languages in use, but in Turing ofcourse. I am trying to create a module to create a textarea, except I would like to have the variables saving the data to have a prefix of which is named by the passed in variable to the procedure being called outside the module. Ahh it looks horific at first, but I'll make it simpler as to what I want.

Turing:

var testVar : string := "testVar2"
var [testVar] : int := 2
%This var will instead be named the value of testVar /'testVar2'/

What is the problem you are having?
I do not know if it is possible to complete the task I need. I need to assign the name of the variable based off another variables value, is that possible?

Describe what you have tried to solve this problem
Tried surrounding the variable name I'm throwing as the name :
Turing:

var /testVar/
var *testVar*

As shown above I've tried different characters hoping to pass by the correct syntax, if it happens to exist.

Please specify what version of Turing you are using
4.1 (School Released Version)
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: Tue Mar 27, 2012 12:56 pm   Post subject: RE:Naming a variable based off the value of another variable. (Horrific)

You can only use letters and numbers in the name of a variable. Also underscores. And periods in some cases.

you could do something like
var testString : string
var testInt : int

or

var testVar1
var testVar2
Dreadnought




PostPosted: Tue Mar 27, 2012 12:57 pm   Post subject: Re: Naming a variable based off the value of another variable. (Horrific)

Variable names must be known at compile time and are immutable, therefore you can't directly achieve what you are trying to do.

If you want to create bindings dynamically (which appears to be what you are trying to do), you would have to create you own data structure that stored both the identifier (the name) and the value (this assumes you know what type of value you want to store).

I'm not entirely sure why you want to do this, or even if you really want to do this.
smool




PostPosted: Tue Mar 27, 2012 5:48 pm   Post subject: RE:Naming a variable based off the value of another variable. (Horrific)

Actually I have run into situations were I originally thought that what ModsCentral is trying to do would be the best way to do it. Everytime, I found a much easier way to do it a different way.
Raknarg




PostPosted: Tue Mar 27, 2012 6:57 pm   Post subject: RE:Naming a variable based off the value of another variable. (Horrific)

Ah, I just got it.
No, that's not possible.
Amarylis




PostPosted: Wed Mar 28, 2012 8:11 am   Post subject: RE:Naming a variable based off the value of another variable. (Horrific)

My advice would be (since I'm assuming that your end-goal is to be able to have a number of variables that are different every time you execute the program) would be to use an array with an unknown upper value that you define earlier in the program. Now, I'm not entirely sure why you'd do that, and I'm fairly sure it's strongly unadvised to do so, but an example of such would be this:

Turing:

var x : int

get x

var arrayX : array 0 .. x of string
ModsCentral




PostPosted: Wed Mar 28, 2012 11:31 am   Post subject: Re: Naming a variable based off the value of another variable. (Horrific)

Actually, what I wanted to do was to create a reusable module, to do something like the following

(Likewise flash (Actionscript 2/3)
textarea.create
textarea.x = 20
textarea.y = 20

textarea is the module, ofcourse, and create/x/y are procedures within, now I've managed to find almost what I want, but I have an array of the names so :
textarea.create(*Name of textarea*, x-coord, y-coord)

Now, i've saved that name to an array, which coorisponds to the x and y coordinates. Now you can modify the x and y using
textarea.x(*Name of textarea*, x-coord)
textarea.y(*Name of textarea*, y-coord)

which is almost what I was looking for, but it works.
Now I have a new question which I'm to lazy to post for help about, I need to run a loop in the module whilst letting the main program run normally.
I need to keep track of the 2 things, the cursor inside the textarea, and if the user pressed a key. And ofcourse I need to loop the cursor to draw and erase on and off, and draw the characters entered.

(Basically I'm redoing the GUI module with more customizing and use out of it with more convienience as a whole.
Dreadnought




PostPosted: Wed Mar 28, 2012 11:45 am   Post subject: Re: Naming a variable based off the value of another variable. (Horrific)

When you say a "resuable module" a class comes to mind. You can define a class, then create objects of that class which each have separate variables. Read the tutorials in the Turing Walkthrough if you're interested.

As for running a loop in the module at the same time as the main program, the best solution is to create a procedure that you place in the loop of the main program. This is in fact what the Turing GUI does (with ProcessEvent).
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: