Discuss Scratch

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…

Last edited by EdgeGamer (May 30, 2013 23:53:11)


Vote for your favourite Kanto starter Pokémon and level it up! Work towards evolving your favourite Pokémon! - http://scratch.mit.edu/projects/25788872/
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.
turkey3
Scratcher
1000+ posts

Variable question

Yes!
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)
The “variable2” is used just to change the variable.

Last edited by turkey3 (May 31, 2013 01:39:45)


scimonster
Scratcher
1000+ posts

Variable question

Harakou wrote:

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.
Correction: You can use (join ), (letter () of ), and (length of ) on numbers.

Retired Community Moderator
BTW, i run Google Chrome 41.0.2272.101 on a Linux system - Ubuntu 14.04. NEW: iPad 4th gen. w/retina.

418 I'm a teapot (original - to be read by bored computer geeks)
THE GAME (you just lost)
; THE SEMICOLON LIVES ON IN OUR SIGS
EdgeGamer
Scratcher
100+ posts

Variable question

Harakou wrote:

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…

Vote for your favourite Kanto starter Pokémon and level it up! Work towards evolving your favourite Pokémon! - http://scratch.mit.edu/projects/25788872/
scimonster
Scratcher
1000+ posts

Variable question

See Changing a Character in a String.

Retired Community Moderator
BTW, i run Google Chrome 41.0.2272.101 on a Linux system - Ubuntu 14.04. NEW: iPad 4th gen. w/retina.

418 I'm a teapot (original - to be read by bored computer geeks)
THE GAME (you just lost)
; THE SEMICOLON LIVES ON IN OUR SIGS

Powered by DjangoBB