egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

calculate % between two rows

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
heman
Voice


Joined: 30 Dec 2006
Posts: 13

PostPosted: Sat Feb 03, 2007 3:00 pm    Post subject: calculate % between two rows Reply with quote

I have to colums: "nm" and "update"

To count the inputs i use the folowing codes:
Code:

set cnt [mysqlsel $db "select count(*) from $table WHERE nm = '$text';" -list]
set cnupdt [mysqlsel $db "select count(*) from $table WHERE update = '$text';" -list]


This gives announcemnet liek this:
Quote:

<botname> 100 releases in db, 50 are updated


Now I want to make a variable that will tell the percentage updates are made from nm

Someting like:
Quote:

<botname> 100 releases in db, 50 are updated (50%)


Thanks
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sat Feb 03, 2007 4:41 pm    Post subject: Reply with quote

You probably would like to investigate the expr command.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
heman
Voice


Joined: 30 Dec 2006
Posts: 13

PostPosted: Sun Feb 04, 2007 7:14 pm    Post subject: Reply with quote

I tried

Code:

set perc [expr { $cnupdt / $cnt * 100 }]


but results is : 0

Am I missing something?
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Feb 04, 2007 8:08 pm    Post subject: Reply with quote

Yup, type conversion and order of operations.
With tcl, if you divide two integers, the result will also be an integer (using truncation). And since percent is less than 1, you'll always get 0.

You'll either have to do the multiplication first, or convert the numbers to floats (I'd say the first is easier)

Try this:
Code:
set perc [expr {($cnupdt*100)/$cnt}]

_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
heman
Voice


Joined: 30 Dec 2006
Posts: 13

PostPosted: Mon Feb 05, 2007 5:44 am    Post subject: Reply with quote

ok that works, sort of.

cnupdt = 7
cnt =175

7 x 100 / 175 = 4 so it announces -> 4


cnupdt = 5
cnt =175

5 x 100 / 175 = 2,86 but it announces -> 2

Is there a way it announce -> 2,86 ?


http://www.tcl.tk/man/tcl8.4/TclCmd/expr.htm
Quote:

expr 5 / 4

returns 1, while

expr 5 / 4.0

return 1.25


I tried
Code:

set perc [expr {($cnupdt.0*100)/$cnt}]

instead of

set perc [expr {($cnupdt*100)/$cnt}]


But that didnt worked.
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Mon Feb 05, 2007 6:08 am    Post subject: Reply with quote

add the ".0" to your static value (100) or remove the braces...
Code:
% set a 3; set b 7
% format %.2f [expr {($a*100.0)/$b}]
42.86
% format %.2f [expr ($a.0*100)/$b]
42.86

_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
heman
Voice


Joined: 30 Dec 2006
Posts: 13

PostPosted: Mon Feb 05, 2007 6:25 am    Post subject: Reply with quote

ok that works, thanks.

Final question Very Happy

the output now looks like for example:

Quote:

2.85714285714 %


Is it possible it wil only announce the first one or two after the "." ?

So someting like:
Quote:

2.85 %


And thanks for all the help
Back to top
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Mon Feb 05, 2007 3:38 pm    Post subject: Reply with quote

Quote:
% format %.2f 2.85714285714
2.86


Edit: user did include that in his code Wink
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
metroid
Owner


Joined: 16 Jun 2004
Posts: 771

PostPosted: Tue Feb 06, 2007 2:24 pm    Post subject: Reply with quote

Using braces in expr is faster so I'd recommend using the first option.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber