How to replace x and y in a string with * and a integer or only an integer depending on a character
Answered
I have a string - for example "2x + 3y + 4 = 0" or "x + 3y + 4 = 0".
And I would like to replace "x" and "y" with "*" and an integer (if there is an integer before it) or just an integer (if there isn't any integer before it).
I guess that I then need to first split the string into a list that contain each single character in the string.
And then get GeoGebra to check which character there is before x and y. And depending on that replace the letter with either "*" and an integer (if there is an integer before it) or just an integer (if there isn't any integer before it).
And then combine each element on the list to a string.
My question: How can I do this ?
I have found another more simple way to do what I want:
First define these 5 variables:
T = "2x + 3y + 4"
T_{After substitution} = ""
ValueOfString__{After substitution} = 0
x_0 = 1
y_0 = 5
Create a button and write the following under its Click Script:
SetValue[ T_{After substitution}, ReplaceAll[ ReplaceAll[ T, "x", "x_0" ], "y", "y_0" ] ] ]
ParseToNumber[ ValueOfString__{After substitution}, T_{After substitution} ]
When the button click script have been run, the value of ValueOfString__{After substitution} changes to 21.
Check out Split(), ReplaceAll() and the UnicodeXXX commands
https://wiki.geogebra.org/e...
A basic idea is to first replace any "<digit><variable>" by "<digit>*<value>", and finally replace any "<variable>" by "<value>".
The first solution attached constructs a list starting with the original string, followed by all the search strings and corresponding replacement strings. Then the Iteration command is used to work through this list, doing all the replacements one after another. The second solution does basically the same but also uses brackets when a replacement value is a negative number (see reasoning below).
The question is what syntax guarantees the original string has, and - assuming that the final string is supposed to be evaluated again be GeoGebra - whether this basic solution is sufficient to produce a string that would evaluate correctly under the given syntax guarantees. A simple example: What if there is white space between a number literal and the following x (or y)? Should the replacement also contain "*" then? Or maybe it would suffice to just add brackets if the replacement value is negative, because in (all?) other cases GeoGebra will interpret the white space as multplication anyway.
Generally, if there are only weak syntax guarantees that would require handling a lot of special cases, then I wouldn't try to accomplish the replacement with GeoGebra commands but do this in JavaScript.
you can replace x with "*" and an integer, then take from second if the first is * , then replace +* with +, then -* with -, then ** with * , etc
I have found another more simple way to do what I want:
First define these 5 variables:
T = "2x + 3y + 4"
T_{After substitution} = ""
ValueOfString__{After substitution} = 0
x_0 = 1
y_0 = 5
Create a button and write the following under its Click Script:
SetValue[ T_{After substitution}, ReplaceAll[ ReplaceAll[ T, "x", "x_0" ], "y", "y_0" ] ] ]
ParseToNumber[ ValueOfString__{After substitution}, T_{After substitution} ]
When the button click script have been run, the value of ValueOfString__{After substitution} changes to 21.
I thought you wanted "2*1+3*5+4"
for 21 you can do T = "2x + 3y + 4" , f(x,y)=x+y , parsetofunction(f,T) , f(x0,y0)
Comments have been locked on this page!