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.

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

Help for those learning Tcl or writing their own scripts.
Post Reply
t
theravenz
Voice
Posts: 7
Joined: Thu Jan 18, 2007 9:50 pm

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

Post by theravenz »

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: Select all

  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..
[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 :P

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
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

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..)
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

You'll get an overflow for integers >= 1<<31 and that error you mentioned for integers >= 1<<32... Try this proc:

Code: Select all

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 :P
Last edited by user on Fri Jan 19, 2007 7:29 am, edited 2 times in total.
Have you ever read "The Manual"?
t
theravenz
Voice
Posts: 7
Joined: Thu Jan 18, 2007 9:50 pm

Post by theravenz »

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
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Make sure you try the edited version. The first proc i posted had an error for values < 1kb :roll:
Have you ever read "The Manual"?
t
theravenz
Voice
Posts: 7
Joined: Thu Jan 18, 2007 9:50 pm

Post by theravenz »

So how am i calling this proc?

do i just do something like

set uploaded bytesToHumanReadable($upload);
t
theravenz
Voice
Posts: 7
Joined: Thu Jan 18, 2007 9:50 pm

Post by theravenz »

Worked it out.. thanks heaps man!!!

You're a Champ!
t
theravenz
Voice
Posts: 7
Joined: Thu Jan 18, 2007 9:50 pm

Post by theravenz »

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..
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

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"?
t
theravenz
Voice
Posts: 7
Joined: Thu Jan 18, 2007 9:50 pm

Post by theravenz »

cheers man

Yeah im not really after 100% values :P just aprox so its all good. :D

thanks again... champ
Post Reply