Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Help with MOD and ROT13 style encoding
- wwaldman
-
Scratcher
2 posts
Help with MOD and ROT13 style encoding
Boy, I'm stuck.
I can't seem to figure out my math error - I'm attempting to encode strings by rotating the values through a table, using MOD to “wrap around”. I'm just experimenting with the letters a through z, and there's a glitch that will not let me encode the last letter.
I'm doing something like ROT13 (actually, you can choose the rotation ) but it can't process characters at the end of the alphabet - so a string that “wraps around” like “abcwxyz” will not encode properly. The value for “z” gets wiped out as it evaluates to 0 and can't then be matched in the table.
Making a special case doesn't seem to work.
Help!
Please see the project at the link.
https://scratch.mit.edu/projects/603174690
Thanks for any help or suggestions!
I can't seem to figure out my math error - I'm attempting to encode strings by rotating the values through a table, using MOD to “wrap around”. I'm just experimenting with the letters a through z, and there's a glitch that will not let me encode the last letter.
I'm doing something like ROT13 (actually, you can choose the rotation ) but it can't process characters at the end of the alphabet - so a string that “wraps around” like “abcwxyz” will not encode properly. The value for “z” gets wiped out as it evaluates to 0 and can't then be matched in the table.
Making a special case doesn't seem to work.
Help!
Please see the project at the link.
https://scratch.mit.edu/projects/603174690
Thanks for any help or suggestions!
- awesome-llama
-
Scratcher
1000+ posts
Help with MOD and ROT13 style encoding
So the problem is due to the numbers returned by the mod block not aligning with the list items. In Scratch, lists count up from 1 rather than 0. If you attempt to get an item outside of that range, you will get a blank output. Item 0 is always nothing. This is what happens to the z - the list instead returns nothing instead of a letter for item 0.
Now to the cause of this. Your script is trying to get the list items 0-25, not 1-26 which is what the list needs. This is because of the mod block, which returns values in that range. A 26 for example will instead become a 0 with it.
The solution to all of this is to just shift the numbers around by 1:

There's a subtraction by 1 just before the mod (the item # of [] in list returns numbers starting from 1) to get it starting from 0. After the mod block, add 1 to get it back in the range suitable for a list to read.
Now to the cause of this. Your script is trying to get the list items 0-25, not 1-26 which is what the list needs. This is because of the mod block, which returns values in that range. A 26 for example will instead become a 0 with it.
The solution to all of this is to just shift the numbers around by 1:

There's a subtraction by 1 just before the mod (the item # of [] in list returns numbers starting from 1) to get it starting from 0. After the mod block, add 1 to get it back in the range suitable for a list to read.
- wwaldman
-
Scratcher
2 posts
Help with MOD and ROT13 style encoding
So the problem is due to the numbers returned by the mod block not aligning with the list items. In Scratch, lists count up from 1 rather than 0. If you attempt to get an item outside of that range, you will get a blank output. Item 0 is always nothing. This is what happens to the z - the list instead returns nothing instead of a letter for item 0.
Now to the cause of this. Your script is trying to get the list items 0-25, not 1-26 which is what the list needs. This is because of the mod block, which returns values in that range. A 26 for example will instead become a 0 with it.
The solution to all of this is to just shift the numbers around by 1:
There's a subtraction by 1 just before the mod (the item # of [] in list returns numbers starting from 1) to get it starting from 0. After the mod block, add 1 to get it back in the range suitable for a list to read.
Thanks for your help!
I restructured the example, and here is the result:
https://scratch.mit.edu/projects/604840244
- Discussion Forums
- » Help with Scripts
-
» Help with MOD and ROT13 style encoding