Discuss Scratch

secretuch
Scratcher
100+ posts

Replace letter () of () with ()

Breck- wrote:

define replace letter (letter) of [text] with [string]
set [i v] to [1]
repeat (length of (text))
add (letter (i) of (text)) to [tmp v]
change [i v] by (1)
end
replace item (letter) of [tmp v] with (string)
set [result v] to (tmp :: list)
on a scale of “simple workaround” to “not simple workaround” i would rate that “very not simple workaround”

-D
Did you forgot
delete all of [tmp v]:: list
?

Last edited by secretuch (May 29, 2024 08:40:53)

IndexErrorException
Scratcher
500+ posts

Replace letter () of () with ()

This needs to be added, the workaround is explanatory but slow because it's written in Scratch, it would speed up many projects a ton if done in JavaScript.
julmik6478
Scratcher
500+ posts

Replace letter () of () with ()

Support, beacuse I support most of new blocks suggestions
gem1001
Scratcher
100+ posts

Replace letter () of () with ()

This seems like it would be simple to implement, as JavaScript (which Scratch is based on) has this function:
x.replace('y', 'z')

Last edited by gem1001 (Oct. 15, 2024 19:00:48)

medians
Scratcher
1000+ posts

Replace letter () of () with ()

You could do this I think:
define replace letter (number1) of (string1) with (string2)
set [result v] to []
set [index v] to [1] //keep track of the letter that is used
repeat (length of (string1))
if <(index) = (number1)> then
set [result v] to (join (string) (string2))
else
set [result v] to (join (string) (letter (index) of (string1))
end
change [index v] by (1) //go to the next letter
end
If you're only replacing it with a single character, this too:
define replace letter (number1) of (string1) with (string2)
set [result v] to []
repeat (length of (string1))
if <(number1) = (length of (result))> then
set [result v] to (join (string) (string2))
else
set [result v] to (join (string) (letter ((length of (result)) + (1)) of (string1)))
end
end
The length of result is used to keep track of the index.
1 is added because Scratch has 1 as the first index.

secretuch wrote:

Did you forgot
delete all of [tmp v]:: list
?
You get the point though, and you quoted the wrong person.
Anyways, to the person that you did quote, it's a pretty simple workaround.

Last edited by medians (June 9, 2024 17:21:15)

medians
Scratcher
1000+ posts

Replace letter () of () with ()

medians wrote:

AndrewB1501 wrote:

-ErrorPurpl_157 wrote:

BossBoss2011 wrote:

I aggree,
<() contains [] :: operators>
should exist!
-snip-
It exists, check the editor
It didn't exist until 3.0, and that is a post from 2.0. It wasn't there back then, but it is now.
Can confirm, person with the same profile picture as me.
Anyway, maybe these blocks alongside this?
(index of [t] in [string] ::operators)
(index of [thing] in [list v] ::list)
I made this post a while ago (the list one already exists)
I think it should be this though (which I don't think exists yet)
((1)th index of [o] in [world] ::operators)
((2)th index of [l] in [Hello world!] ::operators)
((4)th index of [item] in [list v] ::list)
There's already a topic, but I think this would make sense to include alongside this, since you can do:
set [result v] to (replace letter ((1)th index of [p] in [wprld] ::operators) with [o] ::operators)

Last edited by medians (June 9, 2024 17:37:30)

julmik6478
Scratcher
500+ posts

Replace letter () of () with ()

AlexAteThat
Scratcher
16 posts

Replace letter () of () with ()

I made this: (it was a fun (short) side-project)

when green flag clicked
set [ data] to [00000]
replace (1) of variable 'data' with (5) // Change '1' with what you want to replace, set '5' to what you want to replace it with.
say (data)

define replace [ # ] of variable 'data' with [ string ]
set [ count ] to [ 0 ]
set [ store ] to [ ]
repeat (length of (data))
change [ count] by (1)
if <(count) = (#)> then
set [ store] to (join ( store) (string))
else
set [ store] to (join (store) [0]) // Replace '0' with the default character.
end
end
set [ data] to (store)

Credit to @AlexAteThat

Last edited by AlexAteThat (June 23, 2024 07:00:46)

Minoru07
Scratcher
100+ posts

Replace letter () of () with ()

NanoRook wrote:

Breck- wrote:

define replace letter (letter) of [text] with [string]
set [i v] to [1]
repeat (length of (text))
add (letter (i) of (text)) to [tmp v]
change [i v] by (1)
end
replace item (letter) of [tmp v] with (string)
set [result v] to (tmp :: list)

that isn't a simple workaround at all.

Semi-Support, it's obvious this could help but I can't think of anything.

I actually find this workaround to be quite sinple, I'd say it's far simpler than any workaround I could've come up with. It's easy to understand whats going on in the script unlike some other code I've seen.

No support, workaround isn't too hard, and if it is too hard for you then you likely wouldn't need to use the block for anything useful. I'd rather see ST pour rescources into more useful blocks that don't even have remotely simple workarounds.
IndexErrorException
Scratcher
500+ posts

Replace letter () of () with ()

Workaround is very simple, but that doesn't mean it shouldn't be added. Scratch is so slow, if you are trying to insert things to into really long strings, it can take a long time.

Powered by DjangoBB