Words to numbers

id: 751239

category: Help with Scripts

posts: 6

zacharyn35 zacharyn35 loading
How would you make a code so that any words would be changed into numbers and then be able to be changed back again?
-stxllxr -stxllxr loading
You can use lists to do this, have a list with all the numbers you want to convert into words, and vice versa.
say you want the word “hello” to convert into 1, and the word “goodbye” to convert into 2, you can have a list like this:
list:
item 1: hello
item 2: goodbye

then you can use these 2 blocks to convert back and forth:
(item ( v) of [list v] :: list)
(item # of () in [list v] ::list)
And here is how you can convert them back and forth. This is basically a primitive version of decoding and encoding, I would recommend searching up or researching it.
define convert thing (t) into number
set [thing v] to (item # of (t) in [list v] ::list)

define deconvert number (n) into thing
set [thing v] to (item (n) of [list v])
Using the list shown above, and the blocks above, you can convert a word using items in a list, and convert them back.

Edit:
Using these blocks like this:
convert thing [goodbye] into number ::custom
would convert goodbye into “2”, since goodbye is item 2 of the list, and deconverting it would use the item number to then again get the original item, “goodbye”.
zacharyn35 zacharyn35 loading

-stxllxr wrote:

You can use lists to do this, have a list with all the numbers you want to convert into words, and vice versa.
say you want the word “hello” to convert into 1, and the word “goodbye” to convert into 2, you can have a list like this:
list:
item 1: hello
item 2: goodbye

then you can use these 2 blocks to convert back and forth:
(item ( v) of [list v] :: list)
(item # of () in [list v] ::list)
And here is how you can convert them back and forth. This is basically a primitive version of decoding and encoding, I would recommend searching up or researching it.
define convert thing (t) into number
set [thing v] to (item # of (t) in [list v] ::list)

define deconvert number (n) into thing
set [thing v] to (item (n) of [list v])
Using the list shown above, and the blocks above, you can convert a word using items in a list, and convert them back.

Edit:
Using these blocks like this:
convert thing [goodbye] into number ::custom
would convert goodbye into “2”, since goodbye is item 2 of the list, and deconverting it would use the item number to then again get the original item, “goodbye”.
Thanks! Also do you have a code so that it changes it to numbers based on the letter. (Ex. Hi would be 9,10)
to change a string (text) to a number you'd create a list containing every character (letter, number, symbol, etc) you want to support and loop through the whole string, joining each letter's position in the list with the number (possibly adding leading zeroes to one digit numbers to make converting back easier: 7 -> 07)

converting back would require looping through the whole number and grabbing every pair of numbers and joining that position in the list with the current string
-stxllxr -stxllxr loading

zacharyn35 wrote:

Thanks! Also do you have a code so that it changes it to numbers based on the letter. (Ex. Hi would be 9,10)
Yep, that's a little more complicated version of the code above. Doing so is a similar process, but instead of full words in a list, it's individual letters for each item. To simplify it, you should make a script like this to fill the list, using a variable:
define setup list
delete all of [list v] :: list
set [i v] to (1)
repeat (9)
add () to [list v] // needed for encoding, I'll explain later
end
repeat (length of [abcdefghijklmnopqrstuvwxyz1234567890-_ ]) // add more letters as you like.
add (letter (i) of [abcdefghijklmnopqrstuvwxyz1234567890-_] ) to [list v] // there is a space here.
change [i v] by (1
This will add every letter of the alphabet, and will add numbers and a few other special characters if you wanted them. If you want to add more letters, just add more to both the top and bottom part of the “abcde…” part.

The reason we need 9 empty values at the start is because when you encode letters above j (letter 10 of the alphabet) it would encode into “10” and a would encode into “1” so it wouldn't know the difference between ab (1 2) and n (12). Adding in 9 blank values stops this problem, and allows us to encode far easier.

To encode like this, it just involves repeating over every character of the thing you want to encode, and convert it into numbers. The block shown below is how you do so in code, with one extra variable to hold the now converted thing.
define encode thing (t)
set [i v] to (1)
repeat (length of (t))
set [encoded string v] to (join (encoded string) (item # of (letter (i) of (t)) in [list v] ::list
change [i v] by (1)
end
set [encoded string v] to (join (encoded string) [|
The “|” characters is our delimiter, allowing the code when to stop reading and knowing that there is a new value.

Decoding is almost as simple, but we need a few block to do so. Some of the core blocks needed are getting a letter, and getting a value, as shown below, requiring a few new variables.
(let)
(val)
define get letter
set [let v] to (letter (i) of (encoded string))
change [i v] by (1)

define get value
get letter
set [val v] to () // empty
repeat (2)
set [val v] to (join (val) (let))
These 2 blocks allow us to read a single letter of the encoded string, and a single letter of the decoded string. The process to decode an entire string is just to read every encoded letter until you reach the delimiter, or the end point, with another variable. (I know, alot of variables)
define read string
set [decoded string v] to () // empty
repeat until <(let) = [|]>
get value ::custom
set [decoded string v] to (join (decoded string) (item (val) of [list v]))
And that's it for encoding and decoding! You can use the blocks like this:
set [encoded string v] to () //whenever you reset the string, set it to empty.
encode thing [hello] ::custom
encode thing [how are you] ::custom
encode thing [bye] ::custom

set [i v] to (1) // when you want to start at the beginning.
read string :: custom
say (decoded string) for (2) secs
read string :: custom
say (decoded string) for (2) secs
read string :: custom
say (decoded string) for (2) secs
This will say “hello” “how are you” “bye”. Hope this helps!
zacharyn35 zacharyn35 loading

-stxllxr wrote:

zacharyn35 wrote:

Thanks! Also do you have a code so that it changes it to numbers based on the letter. (Ex. Hi would be 9,10)
Yep, that's a little more complicated version of the code above. Doing so is a similar process, but instead of full words in a list, it's individual letters for each item. To simplify it, you should make a script like this to fill the list, using a variable:
define setup list
delete all of [list v] :: list
set [i v] to (1)
repeat (9)
add () to [list v] // needed for encoding, I'll explain later
end
repeat (length of [abcdefghijklmnopqrstuvwxyz1234567890-_ ]) // add more letters as you like.
add (letter (i) of [abcdefghijklmnopqrstuvwxyz1234567890-_] ) to [list v] // there is a space here.
change [i v] by (1
This will add every letter of the alphabet, and will add numbers and a few other special characters if you wanted them. If you want to add more letters, just add more to both the top and bottom part of the “abcde…” part.

The reason we need 9 empty values at the start is because when you encode letters above j (letter 10 of the alphabet) it would encode into “10” and a would encode into “1” so it wouldn't know the difference between ab (1 2) and n (12). Adding in 9 blank values stops this problem, and allows us to encode far easier.

To encode like this, it just involves repeating over every character of the thing you want to encode, and convert it into numbers. The block shown below is how you do so in code, with one extra variable to hold the now converted thing.
define encode thing (t)
set [i v] to (1)
repeat (length of (t))
set [encoded string v] to (join (encoded string) (item # of (letter (i) of (t)) in [list v] ::list
change [i v] by (1)
end
set [encoded string v] to (join (encoded string) [|
The “|” characters is our delimiter, allowing the code when to stop reading and knowing that there is a new value.

Decoding is almost as simple, but we need a few block to do so. Some of the core blocks needed are getting a letter, and getting a value, as shown below, requiring a few new variables.
(let)
(val)
define get letter
set [let v] to (letter (i) of (encoded string))
change [i v] by (1)

define get value
get letter
set [val v] to () // empty
repeat (2)
set [val v] to (join (val) (let))
These 2 blocks allow us to read a single letter of the encoded string, and a single letter of the decoded string. The process to decode an entire string is just to read every encoded letter until you reach the delimiter, or the end point, with another variable. (I know, alot of variables)
define read string
set [decoded string v] to () // empty
repeat until <(let) = [|]>
get value ::custom
set [decoded string v] to (join (decoded string) (item (val) of [list v]))
And that's it for encoding and decoding! You can use the blocks like this:
set [encoded string v] to () //whenever you reset the string, set it to empty.
encode thing [hello] ::custom
encode thing [how are you] ::custom
encode thing [bye] ::custom

set [i v] to (1) // when you want to start at the beginning.
read string :: custom
say (decoded string) for (2) secs
read string :: custom
say (decoded string) for (2) secs
read string :: custom
say (decoded string) for (2) secs
This will say “hello” “how are you” “bye”. Hope this helps!
Thanks!