The other day, while I was programming a project, I discovered a neat little trick to saving space.

By defining a string, such as:

Code:
"1110110001"->Str 1

You can use this:

Code:
Exp(StrMid(Str 1,<1>,<2>))

to retrieve the value of a specific number of that string, for use for various things, defining a variable, checking a condition, etcetera.
Simply enough, <1> is a placeholder for the value, variable, or equation that determines which part of the string you need, while <2> is the number of digits in the resulting number.

The method of writing a value to the string without overwriting any other part of it is a bit hacky, but on the other hand, due to the nature of strings, you can change the length of the string at will(And you can even use StrLen to return a value equal to the dynamic string length). To write a value, you need to use a twisted mess of String commands, like so:

Code:
StrJoin(StrJoin(StrLeft(Str 1,<1>),"<2>"),StrRight(Str 1,<3>))->Str 1

where <1> is the number of the character immediately preceding the beginning of the value to be inserted, <2> is the value to be inserted, and <3> is the number of the character immediately following the inserted value.

This is where a simultaneous pro and con come into play. It is impossible to have a variable's value directly input as <2>. There are two options to work around this: The first, probably less desirable option, is to have a separate clause for each and every possible value of that variable. If there are only a few possible values, then this option becomes more viable than the next, however.
The second option employs a unique feature, to have the variable letter directly input into the string(E.G.: A as <2>). When using the prior command to retrieve a value from the string, the Exp( command retrieves the actual value of the variable A at the time it executes. This allows for another dynamic level that isn't accessible by lists, but could also function as a con, because the variable's value isn't technically saved to the string itself. It will also only work, if and only if, <2> in the value retrieving function equals 1. If the Exp( command reads "100A", it'll result in an error because it doesn't know how to handle it.



The overall pros of this include:
    -Save up to 2800 bytes of space in the RAM by substituting a 255 long string for a 255 long list. That's huge, given how small of storage space you have.
    -Length not explicitly defined, unlike lists so no out-of-domain errors, and length can be changed as you please.
    -Access to more dynamic functions than lists, such as: StrInv(, which reverses the order of the entire string; StrRotate(, which puts the last character onto the front; StrJoin(, with which you can combine two separate strings; or StrLen(, which returns a value equal to the length of the string.


While the overall cons include:
    -Only enough room for a cumulative 255 digits of all values in the string; the number of digits directly determines the maximum amount of values one string can have.
    -Each string should have a predetermined maximum number of digits.
    -A decimal counts as its own digit. As does a negative sign.
    -The code used is more complex than lists, except for when defining a string.
    -Tricky to insert a specific value into a string albeit doable.


I see the greatest use of this, when you have a large list of variables that you know for sure will only be integers 0-9, perhaps a consolidated list of Boolean values.



Edit: As nsg brought up:
nsg wrote:

You can also use + instead of StrJoin and StrMid here would work beter then StrRight. For example to insert "X" at position I:

Code:
StrLeft(Str 1,I-1)+"X"+StrMid(Str 1,I+1)->Str 1


And if you need to inser digit which is in, say, A, then

Code:
StrMid("0123456789",A+1,1)


These allow the elimination of several cons, making this method all the better.
Sarah wrote:

Code:
StrJoin(StrJoin(StrLeft(Str 1,<1>),"<2>"),StrRight(Str 1,<3>))->Str 1



You can also use + instead of StrJoin and StrMid here would work beter then StrRight. For example to insert "X" at position I:

Code:
StrLeft(Str 1,I-1)+"X"+StrMid(Str 1,I+1)->Str 1


And if you need to inser digit which is in, say, A, then

Code:
StrMid("0123456789",A+1,1)
Huh, that would actually work! Did not think of that.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement