Jump to content

Passing operator in function.


Recommended Posts

Hey guys!

Such an awesome program, figured if I'm going to be working with it, I need to join up on the forums and ask questions for problems I can't find an answer for.. Here's the dealio:

I'm trying to pass an operator through a function call. It's basic math, with some formatting applied, so I'm gonna try to simplify the code into basic form.. I may be using terrible coding methods, but as I progress it tends to get better.

Func math(byref $op, byref $val)
    $value1 = GUICtrlRead($Input1, 1)
    $value2 = StringTrimLeft($value1, 1)
    $value3 = $value2 $op $val))
EndFunc

call:

math("+", "7.50")

Something simple will probably cure the error that I keep getting, I've been searching for a while without much luck. It's a pita to find anything without being able to get specific.. Thanks in advance for anyone thats willing to help another noob. :mellow:

Link to comment
Share on other sites

Like this:

;~ Func math(byref $op, byref $val)             ; no real need to pass ByRef as you don't modify these arguments
Func math($op, $val)
;~  $value1 = GUICtrlRead($Input1, 1)       ; let's simplify this into a fixed value for testing
;~  $value2 = StringTrimLeft($value1, 1)        ; why it that here? Let's forget it for now
    Local $value1 = 123.456
    Return(Execute($value1 & $op & $val))
EndFunc

ConsoleWrite(math("+", "7.50") & @LF)

Edit: :mellow:

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Like this:

;~ Func math(byref $op, byref $val)             ; no real need to pass ByRef as you don't modify these arguments **ADD - Good to know.. Thanks!
Func math($op, $val)
;~  $value1 = GUICtrlRead($Input1, 1)       ; let's simplify this into a fixed value for testing
;~  $value2 = StringTrimLeft($value1, 1)        ; why is that here? Let's forget it for now. **ADD - This removes a currency symbol from the input box text value.
    Local $value1 = 123.456
    Return(Execute($value1 & $op & $val))
EndFunc

ConsoleWrite(math("+", "7.50") & @LF)

Edit: :mellow:

Added some explanations to the comments in your post.. Thanks for the help man!

lulz, r u using autoit to make a lisp interpreter or somefink?

Eh? Not sure I understand your pun.. Thanks though...

The end result is this;

Func math($var1, $var2, $var3)
    $val1 = GUICtrlRead($var1, 1); Read InputBox1
    $val2 = StringTrimLeft($val1, 1); Remove $ from InputBox1.Value
    $val3 = Execute($val2 & $var2 & $var3); Execute expressions
    GUICtrlSetData($var1, "$" & stringformat("%.2f", $val3)); Display result in InputBox1
EndFunc

math($InputBox1, "+", "7.50")

I'm sure it's probably sloppy as far as standards go but, 1 group is as follows:

1 input box - text set at "$0.00"

2 buttons - 1 button adds to input box text, the other subtracts, keeping the text formatted with 2 decimal places and the monies symbol.

GUI will contain 20+ input/button groups, different prices assigned to each.

With a little time I should be able to trim the code down even more, any advice is welcome though.. Thanks again for the help guys!

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