Pulling numbers from a string?

I don’t mind. :grin:


Yes! :joy:

*params tells the subroutine that it’s expecting some parameters. Listing the parameters after the command is short way of declaring local temporary variables to hold the values you pass to the subroutine.

So when you call the subroutine like this:

*gosub slice "XXXX42XXXX" 5 2

It’s going to assign XXXX42XXXX to original_string, 5 to starting_index and 2 to len. What these parameters mean is that I want the subroutine to extract a substring from XXXX42XXXX, starting at index/position 5 and which has 2 characters length. So it will loop though the original string and concatenate character by character to a temporary string we declared inside the subroutine called substring until it either reaches the end of the original string or the substring has the required length (2).

Try doing this on your own:

*gosub say_hi "Phenrex"

*finish

*label say_hi
*params name
Hi, ${name}!
*return

That’s right. We would be at position 21 of the string, counting each character as a position.


In ChoiceScript & is the concatenation operator, that’s a fancy way of saying it’s “adding” strings together. So it takes the character at the position of the initial index, in your example it would be 2 and “adds” it so the variable substring. Next iteration it moves one position further and “adds” the next character to substring, which is 1 in your example. By the end the value of substring should be 21.

Try this on your own:

${ "Hello, " & "Phenrex" }

The next step would be something like what @Speedcubing_Gaming posted above. You are absolutely right that in doing things this way you can’t avoid a verbose code, since ChoiceScript has a limited set of data types.

@Speedcubing_Gaming’s suggestion of using array notation is, in my opinion, the most elegant way while still keeping it simple (you can also try to emulate arrays with strings like @Twiger_Fluff did, but it’s more complex and involves loops inside loops :sweat_smile:). To identify the type of cat for example, you’d need to create a numbered list of variables, one variable for each type of cat. This is called a pseudo-array.

*temp cat_type_1 "American Curl"
*temp cat_type_2 "Bengal"
*temp cat_type_3 "Oriental Longhair"
.
.
.
*temp cat_type_12 "Persian"

Then, you look up the value using array notation:

*set breed_cat_typed cat_type[type]

For type you pass the value you extricated previously from the long string containing all the values. The syntax cat_type[type] is the same as writing cat_type_12 if type is 12. But note we can look up variables dynamically this way. This is called “array notation”. You can already shed a few lines of code with this, but yeah, you still have to write that much.


Finally, following the same logic, to load all values, you’d need a pseudo-array with the names of the variables and a control variable with the expected size of the array. This is in effect what @Speedcubing_Gaming did above.

*temp my_var_len 42

*temp my_var_1 "name"
*temp my_var_2 "surname"
*temp my_var_3 "breed_cat_typed"
.
.
.
*temp my_var_42 "skin_color"

Then you’d write a loop to go over the array with the variable names and extricate the corresponding value from the big string containing all values. Supposing the values always have two digits, the the value for name would be the first two digits, the value for surname would be the next two digits, and so own. A simple way to calculate the initial index for each value would be ((current_position - 1) * 2) + 1. So:

  • The value for the first (1) variable starts at 1
  • For the second (2) starts at 3
  • etc

I’m only explaining this so you can understand the underlying logic, but I strongly suggest you take a look at either CSLIB or @Twiger_Fluff’s solution.

For example, I expect you want to allow users to input their own value for name for example, this already breaks the whole code, since name can have an arbitrary length.

1 Like