This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

!calc - calculator script

Old posts that have not been replied to for several years.
Locked
3
3zzy
Halfop
Posts: 42
Joined: Sun Sep 14, 2003 6:58 am

!calc - calculator script

Post by 3zzy »

Is there one out there? not the one which also works as a definition engine, i need only a calculator script which would respond to public commands e.g. !calc 5+5 etc..
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Code: Select all

proc pub:calc {nick uh hand chan arg} {
  set x 0
  set z +
  foreach a [split $arg] {
    switch -- $a {
      {*} -
      {-} -
      {/} -
      {+} { set z $a }
      default {
         if {![regexp {^[0-9]+$} $a]} {
           puthelp "PRIVMSG $chan :Invalid character found"
           return
         }
         set x [expr $x $z $a]
      }
  }
  puthelp "PRIVMSG $chan :Calculated value = $x"
}
bind pub - "!calc" pub:calc
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

secure expr

Post by user »

Here's a safe way to expr stuff (works with math functions like sin/rand/abs etc too). The price you have to pay is less descriptive error messages.

Code: Select all

proc sexpr {e} {
	if {[catch {expr 0+[list $e]} r]} {
		set e "syntax error in expression \"$e\""
	} {
		set e "$e = $r"
	}
}
Have you ever read "The Manual"?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Why not use errinfo to give that description then.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

ppslim wrote:Why not use errinfo to give that description then.
Because of the "0+" making it return the wrong error (the error is captured by catch in my proc, but i don't display it)
Have you ever read "The Manual"?
3
3zzy
Halfop
Posts: 42
Joined: Sun Sep 14, 2003 6:58 am

Post by 3zzy »

Starange behaviour..

[11:55:10] [@3zzy]: !calc 5+5
[11:55:15] [@barabashka]: Invalid character found

[11:54:41] [@3zzy]: !calc 5 + 5
[11:54:43] [@barabashka]: Calculated value = 5
[11:54:45] [@barabashka]: Calculated value = 10

[11:55:34] [@3zzy]: !calc 5 * 5
[11:55:36] [@barabashka]: Calculated value = 5
[11:55:38] [@barabashka]: Calculated value = 25
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Can anybody else solve that one, I am finding it very hard.

Looking at my code, it is near impossible. It would require that the proc physicaly be called twice.

The puthelp line is only called once, and isn't in a loop of any kind.

As for your outputs, 5+5 is invalid, due to the way to does syntax and security checks.

You have two options, you can try using the other script provided, or add this small bit of code below

Code: Select all

set x 0 
  set z +

Code: Select all

if {[llength [split $arg]] == 1} { set arg [join [split $arg {}]] }
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

ppslim wrote:Can anybody else solve that one, I am finding it very hard.

Looking at my code, it is near impossible. It would require that the proc physicaly be called twice.

The puthelp line is only called once, and isn't in a loop of any kind.
Your switch is missing a }, so the putserv is in fact inside the foreach loop ....if you get the code compiled by adding a } to the end (which normal people wouldn't do :P)
ppslim wrote:

Code: Select all

set x 0 
set z +

Code: Select all

if {[llength [split $arg]] == 1} { set arg [join [split $arg {}]] }
!calc 10+10 = 2 if you do that :)
Have you ever read "The Manual"?
Locked