Discuss Scratch

8bitPastime
Scratcher
5 posts

HELP How to set letters in a variable to lowercase

How would I create a script that detects if there is a uppercase letter in a certain variable and automatically makes it lowercase. For example, if I had a block that looked like this:
when green flag clicked
set [ var] to (username)
and the username of the Player was ‘TyLeR’, how would I make it so that the variable was set to ‘tyler’ instead of 'TyLeR.
This sounds like a dumb question but I need this for a font that I am creating

Last edited by 8bitPastime (Oct. 4, 2016 01:27:07)

awesome-llama
Scratcher
1000+ posts

HELP How to set letters in a variable to lowercase

Most of scratch is not case sensitive which is a problem if you wanted to detect case but you don't need to detect case because of this same feature.

You can just iterate over the string and for each character, append the lowercase version to a variable as a new string.

With a block added for Scratch 3.0 there's now quite a simple way of doing it…

define lowercase (string)
set [output v] to [] // blank input
set [i v] to [1] // counter variable
repeat (length of (string))
set [output v] to (join (output) (item (item # of (letter (i) of (string)) in [chars v] :: list) of [chars v] :: list))
change [i v] by (1) // increment counter to go to the next character
end

The list “chars” in the above contains all the characters in their lowercase version as separate list items.
Note that it does not handle unknown characters correctly (it will use the 0th item of the list which is blank) so unknown characters will be lost. This is fine for usernames and places where you know exactly which characters will possibly be seen but in other places, a small modification to the above could be done where the list is checked if it contains the valid character. If not, append the character rather than attempt to find a lowercase version.

Edit (2023): took the opportunity to make this reply better and reword it since the topic is getting viewed by others too.

Last edited by awesome-llama (Jan. 12, 2023 10:58:23)

deck26
Scratcher
1000+ posts

HELP How to set letters in a variable to lowercase

Either use a list containing ‘a’, ‘b’, ‘c’ etc or a text variable ‘abcdefg….’. Then build a copy of your input taking one character at a time and finding it in the list/string. Add the matching character to the end of copy you're building. Since Scratch acts as if ‘T’ = ‘t’ that will make everything lower case. You may need to include other characters like digits or you could just copy them from the original if you didn't find a match.
scubajerry
Scratcher
1000+ posts

HELP How to set letters in a variable to lowercase

gtoal
Scratcher
1000+ posts

HELP How to set letters in a variable to lowercase

8bitPastime wrote:

How would I create a script that detects if there is a uppercase letter in a certain variable and automatically makes it lowercase. For example, if I had a block that looked like this:
when green flag clicked
set [ var] to (username)
and the username of the Player was ‘TyLeR’, how would I make it so that the variable was set to ‘tyler’ instead of 'TyLeR.
This sounds like a dumb question but I need this for a font that I am creating
VERY EASY! because you are converting to lower case rather than swapping upper for lower, you don't need to detect upper or lower case in the input string. Just detect a letter and replace it with the lowercase letter even if it was lowercase to begin with. Loop over the letters in the string one by one and join the replacements into a new string. Create a string called ‘alphabet’ and index into it, ie “abcdefghijklmnopqrstuvwxyz”

Last edited by gtoal (Oct. 5, 2016 06:49:40)

8bitPastime
Scratcher
5 posts

HELP How to set letters in a variable to lowercase

gtoal wrote:

8bitPastime wrote:

How would I create a script that detects if there is a uppercase letter in a certain variable and automatically makes it lowercase. For example, if I had a block that looked like this:
when green flag clicked
set [ var] to (username)
and the username of the Player was ‘TyLeR’, how would I make it so that the variable was set to ‘tyler’ instead of 'TyLeR.
This sounds like a dumb question but I need this for a font that I am creating
VERY EASY! because you are converting to lower case rather than swapping upper for lower, you don't need to detect upper or lower case in the input string. Just detect a letter and replace it with the lowercase letter even if it was lowercase to begin with. Loop over the letters in the string one by one and join the replacements into a new string. Create a string called ‘alphabet’ and index into it, ie “abcdefghijklmnopqrstuvwxyz”

deck26 wrote:

Either use a list containing ‘a’, ‘b’, ‘c’ etc or a text variable ‘abcdefg….’. Then build a copy of your input taking one character at a time and finding it in the list/string. Add the matching character to the end of copy you're building. Since Scratch acts as if ‘T’ = ‘t’ that will make everything lower case. You may need to include other characters like digits or you could just copy them from the original if you didn't find a match.

scubajerry wrote:

https://scratch.mit.edu/projects/10213040/
I figured it out! Thanks!
ZachTChess
Scratcher
33 posts

HELP How to set letters in a variable to lowercase

gtoal wrote:

8bitPastime wrote:

How would I create a script that detects if there is a uppercase letter in a certain variable and automatically makes it lowercase. For example, if I had a block that looked like this:
when green flag clicked
set [ var] to (username)
and the username of the Player was ‘TyLeR’, how would I make it so that the variable was set to ‘tyler’ instead of 'TyLeR.
This sounds like a dumb question but I need this for a font that I am creating
VERY EASY! because you are converting to lower case rather than swapping upper for lower, you don't need to detect upper or lower case in the input string. Just detect a letter and replace it with the lowercase letter even if it was lowercase to begin with. Loop over the letters in the string one by one and join the replacements into a new string. Create a string called ‘alphabet’ and index into it, ie “abcdefghijklmnopqrstuvwxyz”
yeah but how do you get the string itself without the first letter?
(im using words like “Hello”)

Last edited by ZachTChess (Jan. 12, 2023 03:02:11)

deck26
Scratcher
1000+ posts

HELP How to set letters in a variable to lowercase

ZachTChess wrote:

gtoal wrote:

8bitPastime wrote:

How would I create a script that detects if there is a uppercase letter in a certain variable and automatically makes it lowercase. For example, if I had a block that looked like this:
when green flag clicked
set [ var] to (username)
and the username of the Player was ‘TyLeR’, how would I make it so that the variable was set to ‘tyler’ instead of 'TyLeR.
This sounds like a dumb question but I need this for a font that I am creating
VERY EASY! because you are converting to lower case rather than swapping upper for lower, you don't need to detect upper or lower case in the input string. Just detect a letter and replace it with the lowercase letter even if it was lowercase to begin with. Loop over the letters in the string one by one and join the replacements into a new string. Create a string called ‘alphabet’ and index into it, ie “abcdefghijklmnopqrstuvwxyz”
yeah but how do you get the string itself without the first letter?
(im using words like “Hello”)
You'll get help if you create your own new topic instead of necroposting.

Last edited by deck26 (Jan. 12, 2023 10:39:53)

BuiltZackTech1
Scratcher
1 post

HELP How to set letters in a variable to lowercase

So about this topic, does scratch use inputs in a way where “ScRatCH” is the same as “scratch”? Like this:
<[ScRatCH] = [scratch]>True
or will it think it's false, like this:
<[ScRatCH] = [scratch]>False
Because I want to make it so I don't have to do:
if <<(answer) = [Normal]> or <<(answer) = [normal]> or <(answer) = [NORMAL]>>> then
say [Okay, you selected the normal gamemode]
And so on.
end
medians
Scratcher
1000+ posts

HELP How to set letters in a variable to lowercase

Zacktaylor1313 wrote:

So about this topic, does scratch use inputs in a way where “ScRatCH” is the same as “scratch”? Like this:
<[ScRatCH] = [scratch]>True
or will it think it's false, like this:
<[ScRatCH] = [scratch]>False
Because I want to make it so I don't have to do:
if <<(answer) = [Normal]> or <<(answer) = [normal]> or <(answer) = [NORMAL]>>> then
say [Okay, you selected the normal gamemode]
And so on.
end
This is from a long time ago, but the answer block is case-sensitive and always matches what is entered in, while the eqauls block is not.

Powered by DjangoBB