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.
Help for those learning Tcl or writing their own scripts.
heman
Voice
Posts: 13 Joined: Sat Dec 30, 2006 3:17 pm
Post
by heman » Sat Feb 03, 2007 3:00 pm
I have to colums: "nm" and "update"
To count the inputs i use the folowing codes:
Code: Select all
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:
<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:
<botname> 100 releases in db, 50 are updated (50%)
Thanks
nml375
Revered One
Posts: 2860 Joined: Fri Aug 04, 2006 2:09 pm
Post
by nml375 » Sat Feb 03, 2007 4:41 pm
You probably would like to investigate the
expr command.
NML_375
heman
Voice
Posts: 13 Joined: Sat Dec 30, 2006 3:17 pm
Post
by heman » Sun Feb 04, 2007 7:14 pm
I tried
Code: Select all
set perc [expr { $cnupdt / $cnt * 100 }]
but results is : 0
Am I missing something?
nml375
Revered One
Posts: 2860 Joined: Fri Aug 04, 2006 2:09 pm
Post
by nml375 » Sun Feb 04, 2007 8:08 pm
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: Select all
set perc [expr {($cnupdt*100)/$cnt}]
NML_375
heman
Voice
Posts: 13 Joined: Sat Dec 30, 2006 3:17 pm
Post
by heman » Mon Feb 05, 2007 5:44 am
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
expr 5 / 4
returns 1, while
expr 5 / 4.0
return 1.25
I tried
Code: Select all
set perc [expr {($cnupdt.0*100)/$cnt}]
instead of
set perc [expr {($cnupdt*100)/$cnt}]
But that didnt worked.
user
Posts: 1452 Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway
Post
by user » Mon Feb 05, 2007 6:08 am
add the ".0" to your static value (100) or remove the braces...
Code: Select all
% 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"?
heman
Voice
Posts: 13 Joined: Sat Dec 30, 2006 3:17 pm
Post
by heman » Mon Feb 05, 2007 6:25 am
ok that works, thanks.
Final question
the output now looks like for example:
2.85714285714 %
Is it possible it wil only announce the first one or two after the "." ?
So someting like:
2.85 %
And thanks for all the help
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Mon Feb 05, 2007 3:38 pm
% format %.2f 2.85714285714
2.86
Edit: user did include that in his code
metroid
Owner
Posts: 771 Joined: Wed Jun 16, 2004 2:46 am
Post
by metroid » Tue Feb 06, 2007 2:24 pm
Using braces in expr is faster so I'd recommend using the first option.