Jump to content

Eval()


Recommended Posts

Hi all...

The function Eval in AutoIt works unfortunately not the way as I expected it. Translate the Variable names to its values. I know eval only in PHP and linux bash and I do a lot with that helpful function working with templates. Here is what I gladly would like to do.

Imagine I read a textfile template into $template

$name = "Miller"
$customer_number = "12345678"
$template = "Hi Mr. $name - This is your CN: $customer_number"
$text = Eval($template)

I wish the value of $text is now: "Hi Mr. Miller - This is your CN: 12345678"

I know, $text = Eval($template) can't work (I read the doc)

But I wish it would.

$text = Eval("template") also does not give me the values of the variables inside $template

Maybe there is an easy solution but I don'nt see it at the moment.

I can put placeholders in my templates but then I have to do a StringReplace() many times.

Is there another solution to get the values of all variables parsing the string only once?

arctor

Link to comment
Share on other sites

When you want to insert a variable into a string, you can't place it inside the string. Instead, you need to join it with the & operator. Like this:

$template = "Hi Mr. " & $name & " - This is your CN: " & $customer_number

Edit: Correct the $name variable in the same way as the 2nd

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Hi Pekster

When you want to insert a variable into a string, you can't place it inside the string. Instead, you need to join it with the & operator. Like this:

I know. But this:
"Hi Mr. " & $name & " - This is your CN: " & $customer_number
does not work, when I get it from a textfile.

I try to explain it in another way:

$name = "Miller"
$customernumber = "12345678"
$text = FileRead("template.txt", FileGetSize("template.txt"))
;---missing code-------
MsgBox (0, "Output", $text)

Content of template.txt:

Hallo Mr. $name - This is your CN: $customernumber

Bye

I am searching for a solution with or without Eval.

I think there can be 3 ways to get it to work:

1. To concatenate the text and the variables already in the textfile (the method used for strings don't work - but maybe another)

2. To parse the text through a function (eval don't do it, but do it in other languages)

3. To use placeholders instead of variables in the textfile like ---name--- or ---customernumber--- (searching for a method to parse it only once)

I am nearly sure there must be an elegant solution.

arctor

Link to comment
Share on other sites

Oh, I see what you're trying to do. I've created an example that is very flexiable. You can add in as many replacements as you'd like simply by adding in additional variables to the array at the top of my code, seperated by spaces. The for loop will go through each variable you have specified and swap out the text of the variables for what the variables evaluate to.

$replace_array = StringSplit("$name $customer_number", " ")

;--code block here--

$text = FileRead("template.txt", FileGetSize("template.txt"))
For $i = 1 To UBound($replace_array) - 1
  $text = StringReplace($text, $replace_array[$i], Eval($replace_array[$i])
Next

Code fix

And another code fix (see my next post down)

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Thats a good idea with the array

Thanks

$text = StringReplace($text, $replace_array[$i], Eval($replace_array[$i])

In this line I get an Error parsing function call

I will have a look tomorrow. Have to sleep now. 2 AM at my place

thx

arctor

Link to comment
Share on other sites

Sorry, I see my mistake now. The StringSplit function returns the number of items it split into the 1st element of an array (index 0.) This means that you need to modify my example to start at number 1, not 0.

Edit: My above code has been corrected to reflect this.

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

What about this? My testing code is at the bottom.

Func InsertVars($GIVEN)
   Local $POS, $RETVAL
   Local $TMP, $TMPCHAR
   
   $RETVAL = ""
   Do
      $POS = StringInStr($GIVEN, "$")
      If $POS > 0 Then
         $RETVAL = $RETVAL & StringLeft($GIVEN, $POS - 1)
         $TMP = ""
         While 1
            $POS = $POS + 1
            $TMPCHAR = StringMid($GIVEN, $POS, 1)
            If $TMPCHAR = "" Then
               ExitLoop
            ElseIf StringIsAlNum($TMPCHAR) Then
               $TMP = $TMP & $TMPCHAR
            Else
               ExitLoop
            EndIf
         Wend
         If $TMP = "" Then
            $RETVAL = $RETVAL & "$"
         Elseif Not IsDeclared($TMP) then
            $RETVAL = $RETVAL & "$" & $TMP
         Else
            $RETVAL = $RETVAL & Eval($TMP)
            If @Error = 1 Then Return ""
         EndIf
         $GIVEN = StringTrimLeft($GIVEN, $POS - 1)
      Else
         $RETVAL = $RETVAL & $GIVEN
      EndIf
   Until $POS = 0
   Return $RETVAL
EndFunc ;==>InsertVars

Dim $X, $Y, $ABS

$X = 17
$Y = "Abs"
$ABS = "Whatever"

$MSG = InsertVars("The number $x and $$y")
MsgBox(0, "Testing", $MSG)

Edit: Bug fix/feature add to handle missing variables

Edited by Nutster

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Hi all of you,

thanks for all your answers.

I hope I have time to try the code of Valik and Nutster this night.

Pekster:

$text = StringReplace($text, $replace_array[$i], Eval($replace_array[$i])
The parsing error I got is because of one missing ) at the end.

For $i = 1 To UBound($replace_array) -1
is correct just as you said.

But this don't work:

$replace_array = StringSplit("$name $customernumber", " ")

But when I change it to this:

$replace_array = StringSplit("name customernumber", " ")
and take away the $ out of the template, it works. :D

So my resulting code is this:

$name = "Miller"
$customernumber = "12345678"
$replace_array = StringSplit("name customernumber", " ")
$text = FileRead("template.txt", FileGetSize("template.txt"))
For $i = 1 To UBound($replace_array) -1
$text = StringReplace($text, $replace_array[$i], Eval($replace_array[$i]))
Next
MsgBox (0, "title", $text)

The template is this:

Hallo Mr. name - This is your CN: customernumber

Bye

--------------

I will try to get it shorter.

But it's still not a good solution. I have to declare the placeholders and variables new for every template.

Jon:

I was considering adding an ExpandVars option like ExpandEnvVars.

That sounds good. :huh2:

I still believe that the existing eval function should do what the name says. Evaluation.

Maybe I am wrong. I am not a professional programmer.

How does eval work in C++

arctor

Link to comment
Share on other sites

  • 2 months later...

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...