Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding size of scalar

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
GB
Hi All,

How do I find the size (in bytes) of a scalar?

Specifically, I want to find the size of a string (including control chars etc) in memory.

Thanks, rotis23
 
Code:
length($myscalar);
Note this gives the length in characters, not bytes. But it should include the control characters, if any.
 
Thansk stevexff.

What if I had some binary or object data? How could I determine the size (in bytes) used in memory?
 
This maybe?
Code:
require bytes;
print bytes::length( $myscalar );
 
Thanks ishnid.

That only works with scalars. Need to be able to size arrays (I guess I could cycle through) and other non-scalar entities.
 
yes you can loop through:

Code:
my @file = qw(one two three four five six);

{
   use bytes;
   my $bytes = 0;
   for (@file) {
      $bytes += (length $_);
   }
   print "$bytes";
}
 
Another way instead of looping:

Code:
require bytes;
print bytes::length( join("",@myscalar) );


Michael Libeson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top