Discuss Scratch
- Discussion Forums
- » Questions about Scratch
- » Variable question
- EdgeGamer
-
Scratcher
100+ posts
Variable question
Hi. I want to do something with variables but I'm not sure if it can be done.
Let's say I set Variable A to 1723944009319404728 (lots of numbers) and I want to be able to change the 4th character, 3. Is it possible to use the block with the help of other blocks to only change one character of a variable's value?
I was going to put (set letter <3> or (Variable A)) in the slot with the variable name but it doesn't seem to go in…
Let's say I set Variable A to 1723944009319404728 (lots of numbers) and I want to be able to change the 4th character, 3. Is it possible to use the block with the help of other blocks to only change one character of a variable's value?
I was going to put (set letter <3> or (Variable A)) in the slot with the variable name but it doesn't seem to go in…
Last edited by EdgeGamer (May 30, 2013 23:53:11)
- Harakou
-
Scratcher
1000+ posts
Variable question
Well in this case, the variable is an integer rather than a string, so you can't use that block (which only is usable on strings) on it. You can, however, determine the place value of the number and add/subtract a multiple of a power of 10 to change it.
For example, the 4th number in the sequence, 3, is in the 16th place value, so you could change it to 5 by adding
2 * (10^15) to it.
For example, the 4th number in the sequence, 3, is in the 16th place value, so you could change it to 5 by adding
2 * (10^15) to it.
- turkey3
-
Scratcher
1000+ posts
Variable question
Yes!
The “variable2” is used just to change the variable.
set [item#] to (1)
repeat (3) //because the chosen number is 4
set [variable2] to (join (variable2) (letter (item#) of (variable)))
change [item#] by (1)
end //this just means the loop ends
set [variable2] to (join (variable2) (number)) //this is the number that you want to change
set [item#] to (5) //note this is the character you want to change + 1
repeat ((length of (variable)) - (4) //4 because that's the character you want to change
set [variable2] to (join (variable2) (letter (item#) of (variable)))
end
set [variable] to (variable2)
Last edited by turkey3 (May 31, 2013 01:39:45)
- scimonster
-
Scratcher
1000+ posts
Variable question
Well in this case, the variable is an integer rather than a string, so you can't use that block (which only is usable on strings) on it. You can, however, determine the place value of the number and add/subtract a multiple of a power of 10 to change it.Correction: You can use (join ), (letter () of ), and (length of ) on numbers.
For example, the 4th number in the sequence, 3, is in the 16th place value, so you could change it to 5 by adding
2 * (10^15) to it.
- EdgeGamer
-
Scratcher
100+ posts
Variable question
Well in this case, the variable is an integer rather than a string, so you can't use that block (which only is usable on strings) on it. You can, however, determine the place value of the number and add/subtract a multiple of a power of 10 to change it.
For example, the 4th number in the sequence, 3, is in the 16th place value, so you could change it to 5 by adding
2 * (10^15) to it.
The problem is, I need to be able to use this script no matter what number is 4th and set it to 3. Also, no, I don't really need the number 3 as the 4th value, it's just an example =P
I also just realised that I put this in the wrong section…
- Discussion Forums
- » Questions about Scratch
-
» Variable question