How to turn a string into a variable? (TI-84) - ti-basic

I'm trying to turn a string into a variable on my TI-84.
For example,
"XYZ"→Str0
fnInt(X²,sub(Str0,1,1),0,1)→A
But it's not letting me.
I know this seems like a really inefficient way of doing it (why not just do fnInt(X²,X,0,1)?), but in my program this would be very, very useful.
So is there a way to turn a string into a variable?

I don't think you can use a string where fnInt( expects a variable. The only thing I can recommend is to use a bunch of if statements, using I as the index of the variable you're using from your string. Basically, you'd have to write out these cases explicitly, since there's no way to make a string get interpreted as a variable for that function.
If I=1:fnInt(X²,X,0,1)→A
If I=2:fnInt(X²,Y,0,1)→A
If I=3:fnInt(X²,Z,0,1)→A

If you have a string as some function and want to evaluate it for some values, you can always store it to Y1, set the variables in it to what you want, and then just use Y1 as your evaluated function.

Related

UiPath - how to use variables in switch/case

I'm just curious about switch/case in UiPath.
Everywhere else in UiPath, strings must have double quotes or stored in variables, however in switch/case, it seems different.
Every "case" is interpreted as string, regardles of the quotes.
How to use variables in there when they're interpreted as string?
You cant use variable in switch case in UiPath. However you can use following expression in Expression property helps switch dynamically.
Array.IndexOf({var1,var2,var3},item.ToString)
Something like below screenshot
Hope this helps

append or prepend to terraform variable v12

I read that v12 no longer requires interpolation syntax and I'd prefer to not use it whenever possible. One question I have is how do you prepend or append a string to a variable, such as for naming conventions?
with interpolation, you could
name = "${var.variable_name}-vnet"
new v12 syntax supposed to allow variables written like this, but i can't just add on a -vnet as above
name = var.variable_name
The intent of the change in Terraform 0.12 was to avoid the need for redundant interpolation syntax and in particular to avoid the confusion implied by "interpolating" something that isn't a string in order to assign a variable's value to an argument, not to remove the interpolation syntax altogether. If your goal is to concatenate two strings together then interpolation syntax is still likely to be the most concise way to do it, because string concatenation is what the interpolation syntax is for.
There are some other alternatives via built-in functions but it's debatable whether each of them are more or less readable than the interpolation approach:
name = join("-", [var.variable_name, "vnet"])
name = format("%s-vnet", var.variable_name)

Retrieve original string prior to interpolation

I want to write a small patch to String class for my test environment that will warn for usage of double quotes without interpolation (#{foo}).
Is there a way to resolve if a string has derived from interpolation?
If yes, is there a way to retrieve the original un-interpolated value?
There is no "original string prior to interpolation"; a string is created after interpolation has been done. What you are trying to do is impossible at the Ruby level. The only way to do it is to parse the source, as LolWalid suggests, or hack the Ruby code at the C level.

Evaluating a frozen string

My vague understanding is that, with Ruby 2.2's frozen method on string or Ruby 2.3's frozen-string-literal: true pragma, a relevant frozen string literal is evaluated only once throughout program execution if and only if the string does not have interpolation. The following seems to illustrate this:
Not interpolated
#frozen-string-literal: true
5.times{p "".object_id}
Outputs (same object IDs):
70108065381260
70108065381260
70108065381260
70108065381260
70108065381260
Interpolated
#frozen-string-literal: true
5.times{p "#{}".object_id}
Outputs (different object IDs):
70108066220720
70108066220600
70108066220420
70108066220300
70108066220180
What is this property (i.e., being evaluated only once) called? It should be distinct from immutability.
Is my understanding of the condition when strings come to have such property correct? Where is the official documentation mentioning this?
Is there a way to make an interpolated string be evaluated only once?
Interning. The strings are said to be interned.
Not completely. It is more like if the interpreter can decide what the value of the string would be before evaluating it. For example, consider:
5.times { puts "#{'foo'}".object_id }
The id is the same even though there is interpolation involved.
No. This is an internal optimization. The main point of Object#freeze is immutability.
UPDATE: Only literal strings get internalized. This is evident here.
I couldn't find the part of the code responsible for interpolation. So I'm not sure why "#{'foo'}" is considered a literal string. Note that wherever this translation occurs, it is on a lower parser level and happens way before any actual processing. This is evident by the fact that String#freeze is mapped to rb_str_freeze, which doesn't call opt_str_freeze.
"Frozen" is not about whether the string is evaluated more than once. It is, you are right, about mutability.
A string literal will be evaluated every time the line containing it is encountered.
The (only) way to make it be evaluated only once, is to put it in a line of source code that is only executed once, instead of in a loop. A string literal in a loop (or any other part of source code) will always be evaluated every time that line of source code is executed in program flow.
This is indeed a separate thing than whether it is frozen/immutable or not, once evaluated.
The accepted answer is kind of misleading. "It is more like if the interpreter can decide what the value of the string would be before evaluating it." Nope. Not at all. It needs to be evaluated. If the string is frozen, then once it IS evaluated, it will use the same location in memory and the same object/object_id (which are two ways of saying the same thing) as all other equivalent strings. But it's still being evaluated, with or without interpolation.
(Without interpolation, 'evaluation' of a string literal is very very quick. With simple interpolation it's usually pretty quick too. You can of course use interpolation to call out to an expensive method though, hypothetically).
Without interpolation, I wouldn't worry about it at all. With interpolation, if you think your interpolation is expensive enough you don't want to do it in a loop -- the only way to avoid it is not to do it in a loop, but create the string once outside the loop.
Ruby docs probably talk about "String literals" rather than "literal Strings". A "String literal" is any String created by bytes in source code (using '', "", %Q[], or any of the other ways of creating strings literals in source code in ruby). With or without interpolation.
So what kinds of Strings aren't created by String literals? Well, a string created by reading in bytes from a file or network for instance. Or a String created by taking an existing string and calling a method on it that returns a copy, like some_string.dup. "String literal" means a string created literally in source code, rather than by reading from external input. http://ruby-doc.org/core-2.1.1/doc/syntax/literals_rdoc.html

dynamically allocate variables in bash

I've encountered a problem in my bash script.
I need to assign new variables according to files in my folder and assign them a number according to the amount of arguments the script gets (whether it's a script or not).
I'm trying to get a script written like this:
n_${array[*]}=`arg_count ${array[*]}`
while arg_count checks how many parameters a script gets.
for further use, I'm going to change those variables if there's a function with different arguments needed.
Thanks in advance!
In general, you can use the declare builtin to accomplish this, because it is a command whose argument is a string that resembles an assignment.
declare "n_${array[*]}=$(arg_count ${array[*]})"
However, note that unless you set IFS appropriately and the array contents are amenable, the expansion of ${array[*]} isn't going to be a string that forms part of a valid identifier.
You probably want to either use an associative array,
declare -A n
n[${array[*]}]=$(arg_count ${array[*]})
or write your code in a programming language that properly supports data structures.

Resources