May 26, 2005 #1 rotis23 Programmer Joined Aug 29, 2002 Messages 121 Location 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
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
May 26, 2005 #2 stevexff Programmer Joined Mar 4, 2004 Messages 2,110 Location GB Code: length($myscalar); Note this gives the length in characters, not bytes. But it should include the control characters, if any. Upvote 0 Downvote
Code: length($myscalar); Note this gives the length in characters, not bytes. But it should include the control characters, if any.
May 26, 2005 Thread starter #3 rotis23 Programmer Joined Aug 29, 2002 Messages 121 Location GB Thansk stevexff. What if I had some binary or object data? How could I determine the size (in bytes) used in memory? Upvote 0 Downvote
Thansk stevexff. What if I had some binary or object data? How could I determine the size (in bytes) used in memory?
May 26, 2005 #4 ishnid Programmer Joined Aug 29, 2003 Messages 1,422 Location IE This maybe? Code: require bytes; print bytes::length( $myscalar ); Upvote 0 Downvote
May 26, 2005 Thread starter #5 rotis23 Programmer Joined Aug 29, 2002 Messages 121 Location GB 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. Upvote 0 Downvote
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.
May 26, 2005 #6 KevinADC Technical User Joined Jan 21, 2005 Messages 5,070 Location US 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"; } Upvote 0 Downvote
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"; }
May 27, 2005 #7 mlibeson Programmer Joined Mar 6, 2002 Messages 311 Location US Another way instead of looping: Code: require bytes; print bytes::length( join("",@myscalar) ); Michael Libeson Upvote 0 Downvote
Another way instead of looping: Code: require bytes; print bytes::length( join("",@myscalar) ); Michael Libeson