2

I need get size of array in perl, but size must to be in bytes. I can not find any function for this. Thanks in advance

1
  • This is a very vague question. Are you after the resident size in memory of just the array itself? The array and its values too? What if those values are themselves references - do you count the data it refers to as well? Or maybe you're more after the number of bytes that would be output if you were to serialise it in some form or other? Commented Dec 7, 2014 at 15:13

1 Answer 1

7

You can do this using the Devel::Size module -> https://metacpan.org/pod/Devel::Size

#!/usr/bin/perl

use strict;
use warnings;
use Devel::Size qw(total_size);

my @arr = (1, 2, 3, "Foo", "Bar", "Baz", [4, 5, 6], {xyz => 2048});

print "Size: ", total_size(\@arr), " bytes.\n";

On my system this prints:

bria@hel:~$ ./size.pl
Size: 765 bytes.
bria@hel:~$
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.