Discuss Scratch

Tetris_Man
New Scratcher
3 posts

Changing only one letter from a variable.

Hello, My name is Tetris_Man And i want to ask something about scripting,

What do i do, so i can do this?

When receive the message ‘'message1’'
Change (Letter (1) from (variable2)) 3

I want to change only one letter from a variable but i cant, how do i fix it?

Last edited by Tetris_Man (April 26, 2016 23:09:03)

Tetris_Man
New Scratcher
3 posts

Changing only one letter from a variable.

when I receive [ v]
set [(letter) of [world]) of [world]) v] to [ ]
zp100
Scratcher
100+ posts

Changing only one letter from a variable.

First of all, welcome to Scratch! I hope you have fun here.

Second, there isn't a simple way to change a specific letter of a word. But there IS a way. It takes some pretty advanced blocks, though.

First, make a custom block. To do this, go to the scratchblock category “More Blocks.” (The purple one.) Click on the button “Make a Block.” You'll have to click “Options” to find the text and number boxes. (In case you don't know, “string input” means text box.) Make a block like this:
set (letter v) of variable with [new letter]::custom
Ignore the little triangle next to the first “letter.” I did that to get around not being able to put text in circular inputs.

Then, select the “Run without screen refresh” checkbox at the bottom. Finally, click OK. When you do, this will appear in the editor:
define set (letter) of variable with [new letter]
Now, go to the scratchblock section “Data.” Click on the button “Make a Variable.” It'll ask you to type in a name. You can name it whatever you want, but I'm gonna call it “word scan.”

Now, click on the button “Make a List,” also under Data. Again, you can name it whatever you want, but I'll call it “word.”

Add these blocks under the “define” block (you'll find most of them in “Data”):
define set (letter) of variable with [new letter]
set [word scan v] to [0]
delete (all v) of [word v]
repeat (length of (variable))
change [word scan v] by (1)
add (letter (word scan) of (variable)) to [word v]
end
replace item (letter::custom) of [word v] with (new letter::custom)
set [variable v] to (word::list)
If you go back to “More Blocks,” you'll see a block like this:
set (1) of variable with []::custom
Drag it into the editor. Replace the “1” with the letter you want to replace. (1 for first letter, 2 for second letter, 3 for third letter, and so on.) Put the letter you want to change it into in the rectangle. When you click the block, it should change the letter in your variable. Make sure you only put one letter in the rectangle, or else it won't work well. Hope this helps!

Last edited by zp100 (April 27, 2016 01:15:22)

helloandgoodbye9
Scratcher
1000+ posts

Changing only one letter from a variable.

This is a slightly more complicated alternative, but it takes out the need for a list. Yes, it can be easier, but only with hacked blocks.
define replace (letter) of (var) to [new letter]
set [temp v] to [] //Make sure its blank
repeat ((letter) - (1))
set [temp v] to (join (temp) (letter ((length of (temp)) + (1)) of (var)))
end
set [temp v] to (new letter)
repeat until <(length of (temp)) = (length of (var))>
set [temp v] to (join (temp) (letter ((length of (temp)) + (1)) of (var)))
end
stop [this script v]
//Now, its easy. After the custom block, you need to set your variable to temp. Im demonstrating it below.
set [test v] to [helloworld.]
replace (1) of (test) to [H]
set [test v] to [temp v]
replace (6) of (test) to [W]
set [test v] to [temp v]
replace (11) of (test) to [!]
set [test v] to [temp v]
(test) //HelloWorld!

Last edited by helloandgoodbye9 (April 27, 2016 01:49:10)

Tetris_Man
New Scratcher
3 posts

Changing only one letter from a variable.

But what if i don't wanna put a value before editing one letter?

What i don't want to do:
set [variable v] to [00]
(set <variable>'s letter (1) to (1))

What i want to simply do:
(ser <variable>'s letter (1) to (1))

Oh btw, the block is not working..

Last edited by Tetris_Man (April 27, 2016 16:53:44)

asivi
Scratcher
1000+ posts

Changing only one letter from a variable.

Tetris_Man wrote:

But what if i don't wanna put a value before editing one letter?

What i don't want to do:
set [variable v] to [00]
(set <variable>'s letter (1) to (1))

What i want to simply do:
(ser <variable>'s letter (1) to (1))

Oh btw, the block is not working..
I'm unsure about that you said means.
Perhaps?
set [variable v] to (join [ ] (variable))
helloandgoodbye9
Scratcher
1000+ posts

Changing only one letter from a variable.

Tetris_Man wrote:

But what if i don't wanna put a value before editing one letter?

What i don't want to do:
set [variable v] to [00]
(set <variable>'s letter (1) to (1))

What i want to simply do:
(ser <variable>'s letter (1) to (1))

Oh btw, the block is not working..
You dont have to do it before! Only after.
ala89
Scratcher
24 posts

Changing only one letter from a variable.

Hi! I just want to change some letters of a variable but the variable is really very long and it's a cloud variable so I want the letters changing very very fast (under 0.01 second). Thanks.
deck26
Scratcher
1000+ posts

Changing only one letter from a variable.

ala89 wrote:

Hi! I just want to change some letters of a variable but the variable is really very long and it's a cloud variable so I want the letters changing very very fast (under 0.01 second). Thanks.
There is no block to change one character of a text variable so you need to either create a new copy and modify as you do so or store it as a list (possibly temporarily).

For speed do this in a custom block with no screen refresh.
zp100
Scratcher
100+ posts

Changing only one letter from a variable.

ala89 wrote:

Hi! I just want to change some letters of a variable but the variable is really very long and it's a cloud variable so I want the letters changing very very fast (under 0.01 second). Thanks.
Just do what it says above. Make a custom block and a new variable. When you make the custom block, remember to check the “Run without screen refresh” checkbox.

define replace letter (letter #) with (new letter)
set [variable v] to [] // Make sure it's BLANK, not a space.
repeat (length of (☁ cloud variable))
if <(length of (variable)) = ((letter #::custom) - (1))> then
set [variable v] to (join (variable) (new letter::custom))
else
set [variable v] to (join (variable) (letter ((length of (variable)) + (1)) of (☁ cloud variable)))
end
end
set [☁ cloud variable v] to (variable)

This is the most efficient way to do this, I think. Still, the project will have lots of lag if the cloud variable is really long and you're doing it over a hundred times a second. You can use Turbo Mode to help a little, although it might mess up the rest of the project. And, you can run the project at phosphorus.github.io. It might just be impossible. If you share the project, though, I might be able to figure out a workaround.
Sollybunny
Scratcher
16 posts

Changing only one letter from a variable.

helloandgoodbye9 wrote:

This is a slightly more complicated alternative, but it takes out the need for a list. Yes, it can be easier, but only with hacked blocks.
define replace (letter) of (var) to [new letter]
set [temp v] to [] //Make sure its blank
repeat ((letter) - (1))
set [temp v] to (join (temp) (letter ((length of (temp)) + (1)) of (var)))
end
set [temp v] to (new letter)
repeat until <(length of (temp)) = (length of (var))>
set [temp v] to (join (temp) (letter ((length of (temp)) + (1)) of (var)))
end
stop [this script v]
//Now, its easy. After the custom block, you need to set your variable to temp. Im demonstrating it below.
set [test v] to [helloworld.]
replace (1) of (test) to [H]
set [test v] to [temp v]
replace (6) of (test) to [W]
set [test v] to [temp v]
replace (11) of (test) to [!]
set [test v] to [temp v]
(test) //HelloWorld

1 problem for after the 1st loop ends it sets temp to new letter meaning it negates all of that do you mean for that bit
set [ temp] to (join (temp) (new letter))
deck26
Scratcher
1000+ posts

Changing only one letter from a variable.

Sollybunny wrote:

helloandgoodbye9 wrote:

This is a slightly more complicated alternative, but it takes out the need for a list. Yes, it can be easier, but only with hacked blocks.
define replace (letter) of (var) to [new letter]
set [temp v] to [] //Make sure its blank
repeat ((letter) - (1))
set [temp v] to (join (temp) (letter ((length of (temp)) + (1)) of (var)))
end
set [temp v] to (new letter)
repeat until <(length of (temp)) = (length of (var))>
set [temp v] to (join (temp) (letter ((length of (temp)) + (1)) of (var)))
end
stop [this script v]
//Now, its easy. After the custom block, you need to set your variable to temp. Im demonstrating it below.
set [test v] to [helloworld.]
replace (1) of (test) to [H]
set [test v] to [temp v]
replace (6) of (test) to [W]
set [test v] to [temp v]
replace (11) of (test) to [!]
set [test v] to [temp v]
(test) //HelloWorld

1 problem for after the 1st loop ends it sets temp to new letter meaning it negates all of that do you mean for that bit
set [ temp] to (join (temp) (new letter))
You're correct.

Powered by DjangoBB