Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 26th, 2001, 11:28 AM
MattWilliams MattWilliams is offline
hmmmm...
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: UK
Posts: 37 MattWilliams User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Smile

any one know how to work out the size of a variable in KBs ?

Reply With Quote
  #2  
Old April 26th, 2001, 11:49 AM
Atrus's Avatar
Atrus Atrus is offline
yet another member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 262 Atrus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Hi!

The (perl builtin) length function returns you the length of a string $somestring in bytes .

my $somestring = 'Hi there';

my $num_bytes = length($somestring);

my $num_kbytes = $num_bytes / 1000;


Greetings

Atrus.


P.S.: If you want to know the size of an entire array or hash, you'd have to loop over it, adding up the values of each element..

Reply With Quote
  #3  
Old April 26th, 2001, 09:25 PM
miroslav miroslav is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: Belgrade, Yugoslavia
Posts: 19 miroslav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to miroslav
just a small rewrite of above code:

my $somestring = 'Hi there';

my $num_bytes = length($somestring);

my $num_kbytes = $num_bytes / 1024;
if($num_kbytes > 1){int($num_kbytes);}

my $num_mbytes = $num_kbytes / 1024;
if($num_mbytes > 1){int($num_mbytes,2)}

this would count even megabytes like this: 12.34 mbs

I was getting bored so sorry if you find this funny

Reply With Quote
  #4  
Old April 27th, 2001, 12:13 AM
Atrus's Avatar
Atrus Atrus is offline
yet another member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2001
Posts: 262 Atrus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Hi!

Of course we don't find that funny

I also apologize for falling for the 1000, 1024 thing without thinking. Thanks for the wake up call


Greetings

Atrus.

Reply With Quote
  #5  
Old April 27th, 2001, 09:16 AM
miroslav miroslav is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2001
Location: Belgrade, Yugoslavia
Posts: 19 miroslav User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to miroslav
hey no problem, I'm glad I came here... I was looking for a decent perl forum since some time ago my ISP canceled comp.lang.perl newsgroup

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Calculating the size in KBs of a string or variable


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway