Posted: Sun Mar 26, 2006 11:05 am Post subject: Changing Variables
Hi,
I have declared an integer variable with the var command. Is there any way to change this variable later in the program, or is there an equivalent command that I can use that is able to overwrite itself?
What I am trying to do is use variables to store x and y values for "for" loops in an animation (e.g. for x:var1..var2). Instead of repeating the code, I have put the code into a process. Now what I want to do is be able to redeclare those variables whenever I "fork" that process.
I hope that was clear enough. I'm really confused overall about Turing (my teacher isn't so good).
Thanks in advance!
Sponsor Sponsor
MysticVegeta
Posted: Sun Mar 26, 2006 11:15 am Post subject: (No subject)
I am sure there is another way of doing it using loops Avoid Processes, also posting your code what you have so far would help. Also, you cannot redeclare variables, I remmeber making a topic about that long time ago. But you can always use other elements of an array
Evdawg
Posted: Sun Mar 26, 2006 11:33 am Post subject: (No subject)
Here's a chunk of the code:
code:
% Send the leaf players in
var ld: int := 0
var lx1: int := 0
var lx2: int := 0
var ly1: int := 0
var ly2: int := 0
process Leaf_Player
for y:ly1..ly2
for x:lx1..lx2
drawbox(x,y,x+6,y+12, blue)
drawbox(x,y,x+12,y+12, blue)
drawfillbox(x,y+13,x+12,y+25, blue)
drawfillmapleleaf(x,y+12,x+12,y+25, white)
for l:0..4
drawline(x-4-l, y+12, x+1-l, y+25, blue)
drawline(x+12+l, y+25, x+17+l, y+19, blue)
drawline(x-8+l, y, x+17+l, y+19, gray)
drawline(x-4,y+l,x-12,y+l, brown)
end for
drawfilloval(5+x, y+32, 6, 6, brown)
delay(ld)
% Erase Behind
drawbox(x,y,x+6,y+12, white)
drawbox(x,y,x+12,y+12, white)
drawfillbox(x,y+13,x+12,y+25, white)
drawfillmapleleaf(x,y+12,x+12,y+25, white)
for l:0..4
drawline(x-4-l, y+12, x+1-l, y+25, white)
drawline(x+12+l, y+25, x+17+l, y+19, white)
drawline(x-8+l, y, x+17+l, y+19, white)
drawline(x-4,y+l,x-12,y+l, white)
end for
drawfilloval(5+x, y+32, 6, 6, white)
end for
end for
end Leaf_Player
delay(2000)
var ld: int := 100
var lx1: int := 640
var lx2: int := 500
var ly1: int := 0
var ly2: int := 0
fork Leaf_Player
var ld: int := 100
var lx1: int := 500
var lx2: int := 500
var ly1: int := 0
var ly2: int := 200
fork Leaf_Player
Thanks for your responce, MysticVegeta. A chunk of my code is above, and it (obviously) does not run because I cannot redeclare the variables like that. So what can I do to maintain the same functionality?
And also, what would I use instead of a process?
Delos
Posted: Sun Mar 26, 2006 1:25 pm Post subject: (No subject)
You cannot redeclare variables, but you can reassign them.
code:
var hello : string := "Hello"
put hello..
hello := " World!"
put hello
As for alternates to Processes, take a stroll along the [Turing Walkthrough] and look out for the signs pointing to Procedures, Functions, and Processes. The former two will give you some interesting insights into what you can do while avoiding the evil processes.
(Processes in general are not evil, just that Turing handles them badly, and it takes a lot of work and nifty control to get them to do what you want them to.)
Evdawg
Posted: Sun Mar 26, 2006 1:56 pm Post subject: (No subject)
That looks like exactly what I needed.
Delos, thanks so much!
Cervantes
Posted: Sun Mar 26, 2006 2:55 pm Post subject: (No subject)
Posted: Sun Mar 26, 2006 4:44 pm Post subject: (No subject)
Yes that the exactly the reason why they are called variables because their value could vary. Otherwise they would just be called constants. Also look at your for loops. suppose there is a loop "for x : 1..n"
The x serves as a variable because its value changes from 1 to n, but you cannot edit it and say that x := 10, because it has restrictions.
chrispminis
Posted: Sun Mar 26, 2006 6:47 pm Post subject: (No subject)
Hmmm, just on the subject of redeclaring here's a little question. If you are in a situation when you declare an array with the upperbound being the value of another variable, so that you could change it easily if multiple arrays used that value. ex.
code:
var chrisRules : int := 10
var chrisRulesALot : array 1 .. chrisRules of int
or
code:
var chrisRules : string
get chrisRules
var chrisRulesALot : array 1 .. length (chrisRules) of int
Say you wanted to loop your program and add in a restart function that returned you to the beginning. If the length of chrisRules changed is it possible to redeclare the array (or reassign) so that chrisRulesALot has updated bounds?
Sponsor Sponsor
MysticVegeta
Posted: Sun Mar 26, 2006 7:26 pm Post subject: (No subject)
Ah, reminds me of Cervantes again, His Flexible Arrays!!! Look for the tuts, let me give a quick walkthrough:
code:
var something := 10
var myArr: flexible array 1..something of int
How the hell do we change it now?
code:
var something := 10
var myArr : flexible array 1 .. something of int
put upper (myArr)
free myArr
put upper (myArr)
var something2 := 11
for x : 1 .. something2
new myArr, x
end for
put upper (myArr)
Cervantes
Posted: Sun Mar 26, 2006 8:13 pm Post subject: (No subject)
chrispminis wrote:
Say you wanted to loop your program and add in a restart function that returned you to the beginning. If the length of chrisRules changed is it possible to redeclare the array (or reassign) so that chrisRulesALot has updated bounds?
If the array is local (as in, local variables - declared within the loop), then sure. The array's upper bounds will change each time through the loop. (Untested.)
However, if the array is global (declared outside the loop), then you have to use a flexible array or some other data structure for this.
MysticVegeta
Posted: Mon Mar 27, 2006 1:33 am Post subject: (No subject)
Yup, Geoff is right (not surprised) Here is the tested version:
code:
var x := 10
loop
var arr : array 1 .. x of int
put upper (arr)
Input.Pause
x += 1
end loop
Martin
Posted: Mon Mar 27, 2006 3:02 am Post subject: (No subject)
You can pass parameters to functions and processes in turing.
instead of doing this:
code:
var x : int := 4
var y : int := 6
proc myProc
put x, " ", y
end myProc
myProc
do this
code:
proc myProc (x : int, y : int)
put x, " ", y
end myProc
myProc(4,6)
MysticVegeta
Posted: Mon Mar 27, 2006 11:17 am Post subject: (No subject)
Yes but then you will have to call the procedure again to change the values of the variables, and if you want to change those values inside from those procedures, then you have to recurse it, even though if your problem can be solved without recursions
Delos
Posted: Mon Mar 27, 2006 3:16 pm Post subject: (No subject)
And recursion is a bad thing? Well, if it spirals out of control yes, but in all generality, recursion will usually optimize your coding experience. Anyhow, parameters are like cheesecake - you just can't go wrong with them. Unless they're badly made, then you'll just get a stomach ache.
MysticVegeta
Posted: Mon Mar 27, 2006 6:03 pm Post subject: (No subject)
no well, I wasnt saying that its a bad thing, I was just saying if the problem can be solved with using simple arrays then why use recursions? They could get slow and you would have to run your entire procedure just to change the values of a few variables?