Discuss Scratch

Cuteys
Scratcher
2 posts

How do I remove first character from a variable?

Hi! I am wondering how I'm able to remove the first character from a variable.

Example:
Input -> “dog”

Output -> “og”

Any help would be appreciated!
deck26
Scratcher
1000+ posts

How do I remove first character from a variable?

You have to copy each letter from the second to the last to a new variable using ‘join’ to append each in turn. Alternatively convert it to a list and delete the first item and you can potentially just set variable to list to load it back as a single string - BUT I did have a bug a while back where even if the list was single characters only it still put spaces between them when I assigned it to a variable which is not what I believe is supposed to happen. The second method is easier in some ways but both involve looping through the input one character at a time.
TrustyRoo
New Scratcher
49 posts

How do I remove first character from a variable?

Hello!

Here is my implementation, sorry if it is rather bulky, I am new to this programming language.

when gf clicked
set [my variable v] to [dog]
set [found first v] to [0]
set [new variable v] to []
set [loop counter v] to [1]
repeat (length of (my varaible))
if <(found first) = [0]> then
set [found first v] to [1]
else
set [new variable v] to (join (new variable) (letter ( loop counter) of (my variable)))
end
change [loop counter v] by [1]
end
set [my variable v] to (new variable)

This will turn my variable from “dog” to “og”
GIitchInTheMatrix
Scratcher
1000+ posts

How do I remove first character from a variable?

TrustyRoo wrote:

Hello!

Here is my implementation, sorry if it is rather bulky, I am new to this programming language.

when gf clicked
set [my variable v] to [dog]
set [new variable v] to []
set [loop counter v] to [1]
repeat (length of (my varaible))
if <(loop counter) > [1]> then
set [new variable v] to (join (new variable) (letter ( loop counter) of (my variable)))
end
change [loop counter v] by [1]
end
set [my variable v] to (new variable)

This will turn my variable from “dog” to “og”
This version should do the same thing with slightly less code.
TrustyRoo
New Scratcher
49 posts

How do I remove first character from a variable?

Oh I left this out sorry, hit post too soon! This is an implementation of deck26's first description, it will loop through the variable and append a new one from the second item on wards.
The variable found first will be checked to 1 after the first loop to eliminate the first item of the string, afterwards it will add the rest of the letters to new variable (looping over the old one and using loop counter to select each one) and set my variable to it.

Thank you deck26 for the implementation description!
TrustyRoo
New Scratcher
49 posts

How do I remove first character from a variable?

25Freyst wrote:

TrustyRoo wrote:

Hello!

Here is my implementation, sorry if it is rather bulky, I am new to this programming language.

when gf clicked
set [my variable v] to [dog]
set [new variable v] to []
set [loop counter v] to [1]
repeat (length of (my varaible))
if <(loop counter) > [1]> then
set [new variable v] to (join (new variable) (letter ( loop counter) of (my variable)))
end
change [loop counter v] by [1]
end
set [my variable v] to (new variable)

This will turn my variable from “dog” to “og”
This version should do the same thing with slightly less code.
Thank you! I forgot I could use the while loop like that, too used to being able to use for loops in other languages

Please use this version, not mine, as it is less bulky!
Cuteys
Scratcher
2 posts

How do I remove first character from a variable?

TrustyRoo wrote:

Hello!

Here is my implementation, sorry if it is rather bulky, I am new to this programming language.

when gf clicked
set [my variable v] to [dog]
set [found first v] to [0]
set [new variable v] to []
set [loop counter v] to [1]
repeat (length of (my varaible))
if <(found first) = [0]> then
set [found first v] to [1]
else
set [new variable v] to (join (new variable) (letter ( loop counter) of (my variable)))
end
change [loop counter v] by [1]
end
set [my variable v] to (new variable)

This will turn my variable from “dog” to “og”
I understand now, thank you so much!
deck26
Scratcher
1000+ posts

How do I remove first character from a variable?

If you set the loop variable to 2 before the loop you don't need the if block to check it is greater than 1.

when gf clicked
set [my variable v] to [dog]
set [new variable v] to []
set [loop counter v] to [2]
repeat until <(loop counter) > (length of (variable))
set [new variable v] to (join (new variable) (letter ( loop counter) of (my variable)))
change [loop counter v] by [1]
end
set [my variable v] to (new variable)

Powered by DjangoBB