Jump to content

Setting a variable from another variable


Recommended Posts

Hello, I was wondering if there is a way to take a list of strings:

Var
Var1
Var_1

and to set their values without knowing beforehand what they are called.

So, I want to take a user input of Var, and store the next input as it's value.

Basically:

$Var = Inputbox.....
$Val = Inputbox......

$NewVar = "{:content:}quot; & $Var

$NewVar = $Val

This, unfortunately, just sets the whole $NewVar to $Val, instead of what is pointed at in it.

Can this be achieved by a function, or by some sort of pointer?

Thanks, John.

EDIT:: Oh my.....would it happen to be "Assign()"?

Edited by darkjohn20
Link to comment
Share on other sites

Yes, I already know how to fully use both, but I don't believe I need them. Also, how would I go about transforming a string equation into an integer one and solving it?

I've done it with StringRegExp and all, but that takes forever. Is there any easier way? or will I have to continue with this method?

In short, I want to parse equations with * + - / ^ etc...

Link to comment
Share on other sites

$Var = Inputbox.....
$Val = Inputbox......

$NewVar = "{:content:}quot; & $Var

$NewVar = $Val

This, unfortunately, just sets the whole $NewVar to $Val, instead of what is pointed at in it.

Hmm lets see here. The fourth line isn't proper syntax and can't work, and the fifth doesn't make any sense together with the rest you said.

Please try to rephrase, and use real syntax.

Yes, I already know how to fully use both, but I don't believe I need them. Also, how would I go about transforming a string equation into an integer one and solving it?

What sort of string equation? If you have a string like this: "1+2+3" then you can use Execute()

Edit: And read up on arrays anyway, that's probably what you need.

Edited by AdmiralAlkex
Link to comment
Share on other sites

Hmm lets see here. The fourth line isn't proper syntax and can't work, and the fifth doesn't make any sense together with the rest you said.

Please try to rephrase, and use real syntax.

Sorry, I don't know how that happened...That isn't how I pasted it, but it was probably because I put it in the wrong spot.

Anyway..I was saying that I KNOW that is wrong, but it is what I am trying to achieve.

I believe my problem is solved though. I can use Assign() and Execute().

And no, like I said before, I don't want Arrays. I've done it with arrays but it isn't what I want.

Thanks for your help!

Link to comment
Share on other sites

By the way, where does the /z in StringRegExp go in:

$E = StringRegExp($Line, "=([0-9]{1,})", 1)

to make sure that it only matches at the end?

Try: "=([0-9]{1,}\z)" Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks.

What would be the best way to get everything in a string after "=" and the end of the line (It is a single line so no breaks)

StringRegExp, or some other string command?

If I should use StringRegExp, how would I go about saying every number, letter, and */+-^?

Thanks for all of your help.

Link to comment
Share on other sites

Thanks.

What would be the best way to get everything in a string after "=" and the end of the line (It is a single line so no breaks)

StringRegExp, or some other string command?

If I should use StringRegExp, how would I go about saying every number, letter, and */+-^?

Thanks for all of your help.

Check the help file under StringRegExp(). The period "." represents any character in a regular expression. Note the section on "Repeating Characters"; for example ".+" translates as "any one or more characters".

Take a shot at the RegExp and post what you got if it doesn't work. This is a nice easy task to start out on, so take advantage of the opportunity to learn a very useful tool!

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Ah yes, I forgot that the . existed! I was about to try something like [0-9][a-z][\+\-\*\/\^] etc....

I got it now. Will try in a sec. Thanks again, sorry for my lack of memory right after school... >.<

My problem is sometimes I don't really think before I ask. I am by no means new to AutoIt and most other languages, but I don't slow down to look for the answer.

Edited by darkjohn20
Link to comment
Share on other sites

wouldn't stringRight(StringLen($str)-StringInStr($str,"=")) be quicker than a regexp?

"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
Link to comment
Share on other sites

wouldn't stringRight(StringLen($str)-StringInStr($str,"=")) be quicker than a regexp?

Should be: StringRight($str, StringLen($str) - StringInStr($str,"="))

But you're right about speed. Takes about half the time of StringRegExp, as long as the pattern is so simple it can be done that way. StringRegExp is much more flexible and powerful for more complicated string patterns, and will pull out multiple matches to an array in one pass, which would require a complicated loop and probably much more time with other String functions.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

One last thing that I can think of.

My string has become more complicated than "Find anything after the '=' and Execute() it".

I need to include variables.

Right now, my string searches "Number Operator Number". This is bad because that means only one operation can be done. You couldn't do 5+5+5 etc...

This is why I asked about searching everything after "=".

But now, say one of those numbers is a previously declared variable:

Var_A = 10

Var_B = 10+Var_A

Would I be able to make a StringRegExp to include ALL equations, or would I have to make one for vars and one for numbers?

Link to comment
Share on other sites

One last thing that I can think of.

My string has become more complicated than "Find anything after the '=' and Execute() it".

I need to include variables.

Right now, my string searches "Number Operator Number". This is bad because that means only one operation can be done. You couldn't do 5+5+5 etc...

This is why I asked about searching everything after "=".

But now, say one of those numbers is a previously declared variable:

Var_A = 10

Var_B = 10+Var_A

Would I be able to make a StringRegExp to include ALL equations, or would I have to make one for vars and one for numbers?

If you really must, you can use Assign()/Eval(). See the help file. But that usually leads to awkward, broken, unmanageable code.

Might be better to learn to use a Scripting Dictionary object (use the forum search on it).

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I take back what I said before, I am using an array to store The variable name and the value, but how can I read both

Var_A=5+5

and

Var_A=Var_A+5

Everything but the "=" can and most likely will be different because these are user defined.

In other words, I need a way to take an equation found with StringRegExp (I have code up to this point)

and replace any variables with their value, and solve it. For normal equations it will just solve.

How do I do:

$Equation = StringRegExp($Line, "=(.+)", 1) ;Gets the whole equation
$FoundVars = StringRegExp($Line, "XXXX") ;Find any variables. Can have nums,letters of both cases, and "_"

If $FoundVars = 1 ; If any vars are found
    $FoundVars = StringRegExp($Line, "XXXX", 1) ;Actually get the vars

    For $X = 0 To UBound($FoundVars)-1 ;For each var
        For $Y = 0 To $VariableCnt-1 ;For each variable
            If $Variables[$Y][0] = $FoundVars[$X] Then ;If the foundvariable matches the existing one
                $New = StringReplace($Equation, $FoundVars[$X], $Variables[$Y][1]
                $Answer = Execute($New)
            EndIf
        Next
    Next
EndIf

This was written quickly, not sure if it is correct, but it is the idea. What would I search for in the SRE for $FoundVars???

Thanks

Edited by darkjohn20
Link to comment
Share on other sites

You need a solid definition of what a variable name looks like. For example, does it always start with "Var_", followed by a single capital letter? If it's always setting a variable on the left of "=", then a simple StringSplit() might be better to separate the left and right parts. But the RegExp "([^=]+)(?:=)([^=]+)" should achieve about the same thing, just for the geekiness of it.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

And also, I already have a method to get both the Left and the Right sides of the "=". I just need a way to tell if on the right it is a variable, and to replace it accordingly.

EDIT::

I will just restrict variables from containing a _ and make it:

$Vars = StringRegExp($Equation[0], "([[:alpha:]]{1,})", 1)

Works now.

Edited by darkjohn20
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...