Re: File Size Calculator

2004-08-10 Thread SilverFox
Jose Alves De Castro wrote:

 On Mon, 2004-08-09 at 14:53, David Dorward wrote:
 On 9 Aug 2004, at 14:34, SilverFox wrote:
 
  Hi all, I'm trying to writing a script that will allow a user to enter
  a
  number and that number will be converted into KB,MB or GB depending on
  the
  size of the number. Can someone point me in the right direction?
 
 What have you got so far? Where are you stuck? Getting user input
 (where from)? Working out which order of magnitude the number is?
 
 I wouldn't do that (the part of finding the order of magnitude)... I
 would probably keep on doing calculations while the numbers was greater
 then 1024... and in the end, when it was, the right letter to append
 would be based on the amount of calculations done...
 
 I remember reading something about this on use.Perl ... it was a while
 ago, and I'm not sure whether it ever got into a module, but the guy had
 written some wonderful code to do this :-)
 
 Converting between kilo and mega et al? Showing the output?
 
 Show us some code.
 
 --
 David Dorward
   http://dorward.me.uk/
 http://blog.dorward.me.uk/

I haven't put anything together as yet. Putting some if/elsif statement 
together would be the easies way I can think off. Something like:

$kilo= 1024;
$Mega= 1048576;
$gig= 1073741824;

print Please enter your number:\n;
chomp($num=STDIN);

if ($num = $gig) 
{
need code to do the convertion/rounding of given number
print you entered: $num\n;
print which is:
} elsif {
continue with the same format

}

The problem i'm having it converting/rounding the inputted number into a 
valid byte (KB/MB/GB) count.

SilverFox

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: File Size Calculator

2004-08-10 Thread mgoland
Quickest wya would be to get the left over from begining.

...

print Please enter your number:\n;
chomp($num=STDIN);

$bytes = $num % $kilo;
$num -= $bytes

...
HTH,
Mark G.

- Original Message -
From: SilverFox [EMAIL PROTECTED]
Date: Monday, August 9, 2004 12:06 pm
Subject: Re: File Size Calculator

 Jose Alves De Castro wrote:
 
  On Mon, 2004-08-09 at 14:53, David Dorward wrote:
  On 9 Aug 2004, at 14:34, SilverFox wrote:
  
   Hi all, I'm trying to writing a script that will allow a user 
 to enter
   a
   number and that number will be converted into KB,MB or GB 
 depending on
   the
   size of the number. Can someone point me in the right direction?
  
  What have you got so far? Where are you stuck? Getting user input
  (where from)? Working out which order of magnitude the number is?
  
  I wouldn't do that (the part of finding the order of 
 magnitude)... I
  would probably keep on doing calculations while the numbers was 
 greater then 1024... and in the end, when it was, the right 
 letter to append
  would be based on the amount of calculations done...
  
  I remember reading something about this on use.Perl ... it was a 
 while ago, and I'm not sure whether it ever got into a module, 
 but the guy had
  written some wonderful code to do this :-)
  
  Converting between kilo and mega et al? Showing the output?
  
  Show us some code.
  
  --
  David Dorward
http://dorward.me.uk/
  http://blog.dorward.me.uk/
 
 I haven't put anything together as yet. Putting some if/elsif 
 statement 
 together would be the easies way I can think off. Something like:
 
 $kilo= 1024;
 $Mega= 1048576;
 $gig= 1073741824;
 
 print Please enter your number:\n;
 chomp($num=STDIN);
 
 if ($num = $gig) 
 {
need code to do the convertion/rounding of given number
print you entered: $num\n;
print which is:
 } elsif {
 continue with the same format
 
 }
 
 The problem i'm having it converting/rounding the inputted number 
 into a 
 valid byte (KB/MB/GB) count.
 
 SilverFox
 
 -- 
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/ http://learn.perl.org/first-response
 
 
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: File Size Calculator

2004-08-10 Thread Brian Gerard
And the clouds parted, and SilverFox said...

 Hi all, I'm trying to writing a script that will allow a user to enter a 
 number and that number will be converted into KB,MB or GB depending on the 
 size of the number. Can someone point me in the right direction?
 
 Example: 
 user enter: 59443
 Script will output: 58M
 
 SilverFox
 

Here's a little chunk that should give you about what you're looking
for, up to Tebibytes (2**40 bytes).  Note that I used the binary
prefixes[1] (Kibi, Mebi, Gibi, Tebi) as opposed to the base-10 versions
(Kilo, Mega, Giga, Tera).  Feel free to change them if you're so
inclined.  :)

--- Begin Chunk ---

our %ByteCount = (
  B = 1,
KiB = 2**10,
MiB = 2**20,
GiB = 2**30,
TiB = 2**40
);


sub prettybyte {
my $bytes = shift;

foreach my $unit ( qw{ TiB GiB MiB KiB B } ) {
if ($bytes = $ByteCount{$unit}) {
return sprintf(%4.3f $unit, $bytes/$ByteCount{$unit});
}
}
}

--- End Chunk ---

HTH[2]-
Brian

[1] http://www.alcyone.com/max/reference/physics/binary.html
-anyone remember offhand the URL to the /. story on these, btw?

[2] I'm a little rushed at the moment, so I don't have time to fill
in any details of how it works.  Let me know if you want/need an
explanation and I'll be happy to provide one.  :)


  /~~\
 | Brian Gerard  Some drink at the fountain of|
 | First initial + 'lists'  knowledge...others just gargle.   |
 | at technobrat dot com  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: File Size Calculator

2004-08-10 Thread Charles K. Clarkson
SilverFox [EMAIL PROTECTED] wrote:

: 
: I haven't put anything together as yet. Putting
: some if/elsif statement together would be the
: easiest way I can think off. Something like:


We can see a few problems right off. All scripts
should start with 'strict' and 'warnings'. We need a
consistent naming convention for variables. If some
start with capitalized letters, there should be a
non-arbitrary reason for doing so.

: $kilo= 1024;
: $Mega= 1048576;
: $gig= 1073741824;


use strict;
use warnings;

my $kilobyte = 1024;
my $megabyte = 1024 ** 2;
my $gigabyte = 1024 ** 3;

 
: print Please enter your number:\n;
: chomp($num=STDIN);

chomp( my $value = STDIN );

: if ($num = $gig)
: {
: need code to do the convertion/rounding
: of given number print you entered:
: $num\n; print which is:
: } elsif {
: continue with the same format
: 
: }
: 
: The problem i'm having it converting/rounding the
: inputted number into a valid byte (KB/MB/GB) count.

I suppose that takes some basic math. To round
to a whole number, we examine the fraction to
determine whether we should adjust the whole number
up or down. It is important to separate the number
from the fraction.

   Luckily, Math::Round has the nearest() function.
To find the nearest number of units, we use this.

nearest( $unit, $number ) / $unit


Here's one solution. It's very generic. One
could easily adopt it for miles, yards, and feet
given a value in feet. I leave error checking to
you.


use strict;
use warnings;

use Math::Round 'nearest';

print Please enter your number:\n;
chomp( my $value = STDIN );

my %units = (
1024  = 'KB',
1024 ** 2 = 'MB',
1024 ** 3 = 'GB',
);

foreach my $unit ( sort {$b = $a} keys %units ) {
if ( $value = $unit ) {
printf %s = %s %s\n,
$value,
nearest( $unit, $value ) / $unit,
$units{ $unit };
last;
}
}

__END__


We still need to handle values smaller than
1024, but this solution might make that easier
to do. It won't handle non-positive values, though.


my %units = (
1024 ** 0 = 'Bytes',
1024 ** 1 = 'KB',
1024 ** 2 = 'MB',
1024 ** 3 = 'GB',
);


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328












-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: File Size Calculator

2004-08-10 Thread Brian Gerard
And the clouds parted, and Brian Gerard said...
 
 [1] http://www.alcyone.com/max/reference/physics/binary.html
   -anyone remember offhand the URL to the /. story on these, btw?
 

...never mind.  Found it.  (uncaught typo on my first google query... DOH!)

http://slashdot.org/articles/99/08/10/0259245.shtml


  /~~\
 | Brian Gerard  Give me liberty or give me something |
 | First initial + 'lists' of equal or lesser value   |
 | at technobrat dot com   from your glossy 32-page catalog.  |
  \__/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: File Size Calculator

2004-08-09 Thread David Dorward
On 9 Aug 2004, at 14:34, SilverFox wrote:
Hi all, I'm trying to writing a script that will allow a user to enter 
a
number and that number will be converted into KB,MB or GB depending on 
the
size of the number. Can someone point me in the right direction?
What have you got so far? Where are you stuck? Getting user input 
(where from)? Working out which order of magnitude the number is? 
Converting between kilo and mega et al? Showing the output?

Show us some code.
--
David Dorward
 http://dorward.me.uk/
http://blog.dorward.me.uk/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response



Re: File Size Calculator

2004-08-09 Thread Jose Alves de Castro
On Mon, 2004-08-09 at 14:53, David Dorward wrote:
 On 9 Aug 2004, at 14:34, SilverFox wrote:
 
  Hi all, I'm trying to writing a script that will allow a user to enter 
  a
  number and that number will be converted into KB,MB or GB depending on 
  the
  size of the number. Can someone point me in the right direction?
 
 What have you got so far? Where are you stuck? Getting user input 
 (where from)? Working out which order of magnitude the number is?

I wouldn't do that (the part of finding the order of magnitude)... I
would probably keep on doing calculations while the numbers was greater
then 1024... and in the end, when it was, the right letter to append
would be based on the amount of calculations done...

I remember reading something about this on use.Perl ... it was a while
ago, and I'm not sure whether it ever got into a module, but the guy had
written some wonderful code to do this :-)

 Converting between kilo and mega et al? Showing the output?
 
 Show us some code.
 
 --
 David Dorward
   http://dorward.me.uk/
 http://blog.dorward.me.uk/
-- 
José Alves de Castro [EMAIL PROTECTED]
  http://natura.di.uminho.pt/~jac


signature.asc
Description: This is a digitally signed message part


Re: File Size Calculator

2004-08-09 Thread Chris Devers
On Mon, 9 Aug 2004, SilverFox wrote:
Example:
user enter: 59443
Script will output: 58M
I know this isn't getting into the spirit of things, but have you 
considered simply using the `units` program?

% units
500 units, 54 prefixes
You have: 59443 bytes
You want: megabytes
* 0.056689262
/ 17.640025
You have: 59443 bytes
You want: kilobytes
* 59.443
/ 0.016822839
You have: ^C
% units bytes kilobytes
   * 0.001
/ 1000
% units bytes megabytes
* 9.5367432e-07
/ 1048576
The nice thing about `units` -- in this context -- is that it lets the 
user pick the conversion units they want to work with, and also gives 
hints for converting both to  from the alternate measurement scale.

Of course, working this into a larger program that does other things 
might be annoying -- in which case your way is better -- but if all you 
want is the conversions, this is a solved problem :-)

--
Chris Devers
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response