Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
That's pretty nice, although it does have a few problems. I wrapped it in a harness and checked a few boundary values:
-1 gives an odd (arguable?) result, 0 gives nothing, and anything from 512 to 1023 is 1K, rather than <1K. Suppose I want to extend it to teras and petas... and beyond? Can you see the redundancy in your code? How could you factor that out? If you did, it would be easier to extend. BTW, my harness looks like this (ugly, but it gets the job done).
Ok, enough blabbering, here's how I would do it. Note that my routine produces different output. I think that is due to the way you employ 512 in your algorithm. I don't think that's necessary, but I'm not paying close attention.
Extending this routing is as simple as increasing the @suffix array, which is as it should be. Note to readers: this routine, like the original both run cleanly under strict and warnings. If you're curious, after peta comes exa (E), zetta (Z) and yotta(Y). Man, think of all the mp3s you could store on a yottabyte, of course, the backups would kill you. Just don't get me started on kibibytes and mebibytes. print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u' In reply to Re: pretty print bytes
by grinder
|
|