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 

TCL IRC Output.. Changing Bytes to KB, MB, ect..

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


Joined: 18 Jan 2007
Posts: 7

PostPosted: Thu Jan 18, 2007 9:58 pm    Post subject: TCL IRC Output.. Changing Bytes to KB, MB, ect.. Reply with quote

Just having some trouble with this coding..

Searched everywhere for help.. hopfully one of you guys can help me out
It'd be much appriciated.

Code:
  if {[expr $upload / 1024] >= 1} {
       set uploaded "[string range "[expr $upload / 1024.0]" 0 [expr [string length "[expr $upload / 1024]"]+ 2] ] KB"};

  if {[expr $upload / 1048576] >= 1} {
       set uploaded "[string range "[expr $upload / 1048576.0]" 0 [expr [string length "[expr $upload / 1048576]"]+ 2] ] MB"};

  if {[expr $upload / 1073741824] >= 1} {
       set uploaded "[string range "[expr $upload / 1073741824.0]" 0 [expr [string length "[expr $upload / 1073741824]"]+ 2] ] GB"};

  if {[expr $upload / 1099511627776] >= 1} {
       set uploaded "[string range "[expr $upload / 1099511627776.0]" 0 [expr [string length "[expr $upload / 1099511627776]"]+ 2] ] TB"};

  if {[expr $download / 1024] >= 1} {
       set downloaded "[string range "[expr $download / 1024.0]" 0 [expr [string length "[expr $download / 1024]"]+ 2] ] KB"};

  if {[expr $download / 1048576] >= 1} {
       set downloaded "[string range "[expr $download / 1048576.0]" 0 [expr [string length "[expr $download / 1048576]"]+ 2] ] MB"};

  if {[expr $download / 1073741824] >= 1} {
       set downloaded "[string range "[expr $download / 1073741824.0]" 0 [expr [string length "[expr $download / 1073741824]"]+ 2] ] GB"};

  if {[expr $download / 1099511627776] >= 1} {
       set downloaded "[string range "[expr $download / 1099511627776.0]" 0 [expr [string length "[expr $download / 1099511627776]"]+ 2] ] TB"};


now im not fantastic when it comes to TCL more of a novice..

When these lines are called.. i get an error like this..

Quote:
[12:53] <ServBot> [12:53] Tcl error [user_search]: integer value too large to represent


I've isolated it to these lines only.. if i comment them out.. it works flawlessly.. cept displayed in bytes Razz

One other thing i noticed is, if i comment out the GB and TB lines and leave KB and MB.. it works for anything under a Gig..
anything over a gig it just returns that error again

Any help would be nice.. thanks in advance..

Matt
Back to top
View user's profile Send private message
rosc2112
Revered One


Joined: 19 Feb 2006
Posts: 1454
Location: Northeast Pennsylvania

PostPosted: Fri Jan 19, 2007 6:04 am    Post subject: Reply with quote

Why don't you use elseif instead, so that the largest numbers are not being divided by the smallest divisor (I'm assuming that is the problem, since you didn't bother to post the actual '.set errorInfo' debug.. Could be the opposit, smallest number being divided by the largest divisor..)
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Fri Jan 19, 2007 7:25 am    Post subject: Reply with quote

You'll get an overflow for integers >= 1<<31 and that error you mentioned for integers >= 1<<32... Try this proc:
Code:
proc bytesToHumanReadable b {
   set b [format %g $b]
   foreach {v n} {1.09951e+012 TB 1.07374e+009 GB 1.04858e+006 MB 1024.0 KB 1.0 B} {
      if {$b>=$v} break
   }
   format "%.2f %s" [expr {$b/$v}] $n
}


EDIT: oops Razz
_________________
Have you ever read "The Manual"?


Last edited by user on Fri Jan 19, 2007 7:29 am; edited 2 times in total
Back to top
View user's profile Send private message
theravenz
Voice


Joined: 18 Jan 2007
Posts: 7

PostPosted: Fri Jan 19, 2007 7:25 am    Post subject: Reply with quote

Even with elseif.. -- thought i had tried that before posting it.. and it didnt seem to like elseif.. but oh wells works now...

Still no go.. comes up with

[22:23] <ServBot> [22:22] Tcl error [user_search]: integer value too large to represent

..

Could it be that TCL just cant handle calculations with such large numbers???
coz thats the only thing i can figure out

Would anyone know of any other code that might be able to help with this calulation that i could try?


PS, new post while posting this.. ill give it a go dude
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Fri Jan 19, 2007 7:33 am    Post subject: Reply with quote

Make sure you try the edited version. The first proc i posted had an error for values < 1kb Rolling Eyes
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
theravenz
Voice


Joined: 18 Jan 2007
Posts: 7

PostPosted: Fri Jan 19, 2007 8:40 am    Post subject: Reply with quote

So how am i calling this proc?

do i just do something like

set uploaded bytesToHumanReadable($upload);
Back to top
View user's profile Send private message
theravenz
Voice


Joined: 18 Jan 2007
Posts: 7

PostPosted: Fri Jan 19, 2007 8:52 am    Post subject: Reply with quote

Worked it out.. thanks heaps man!!!

You're a Champ!
Back to top
View user's profile Send private message
theravenz
Voice


Joined: 18 Jan 2007
Posts: 7

PostPosted: Sat Jan 20, 2007 1:22 pm    Post subject: Reply with quote

Okay, i got a new issue..

I totally dont understand that script you wrote.. hah! my TCL Noobness..

Anyways im just after a calc for ratio..

need to divide $upload by $download to give a 1.000 count..

Say 4 / 2 = 2.000 (to 3 decimal places.)

What im also after to.. is if they havent downloaded it returns "Inf." and if they havent uploaded it returns "---"

Im sure i could code that in if i only got the ratio calc...

If anyone knows how to calc big numbers in this way i would appriciate your help.. I can calc anything up to MB sized numbers in bytes.. but soon as i get up to GB sized numbers in bytes i get that error i was experiencing earlier..
Back to top
View user's profile Send private message
user
 


Joined: 18 Mar 2003
Posts: 1452
Location: Norway

PostPosted: Sat Jan 20, 2007 5:46 pm    Post subject: Reply with quote

Code:
proc ratio {up down} {
   format %.3f [expr {[format %g $up]/[format %g $down]}]
}


Keep in mind that this (and the previous) proc will not give you the exact answer, but it's the best you can do without loading external libraries.
_________________
Have you ever read "The Manual"?
Back to top
View user's profile Send private message
theravenz
Voice


Joined: 18 Jan 2007
Posts: 7

PostPosted: Sun Jan 21, 2007 2:13 am    Post subject: Reply with quote

cheers man

Yeah im not really after 100% values Razz just aprox so its all good. Very Happy

thanks again... champ
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