in reply to How to get the TOTAL length/size of an array?

The title of your question is misleading, because you want the total number of characters of an array of strings

An array has only the "size" of scalar @array because in reality the members are only pointers to scalars.

But a scalar can be a reference to an even deeper data structure.

The correct answer would hence be using Devel::Size to get the total memory of a deep data structure.

If you only want the total length of characters of a deep data structure, you can (ab)use serializers Data::Dumper or similar for an approximation, with the benefit that this would "work" for more deeply nested structures too

edit DEMO

DB<20> use Data::Dumper DB<21> @a=("abc", "cd", "e", "fg", "hi", "hello world"); DB<23> $Data::Dumper::Terse = 1 DB<24> $Data::Dumper::Indent = 0 DB<25> p Dumper @a 'abc''cd''e''fg''hi''hello world' DB<26> p length(Dumper @a)-2*@a 21 DB<27>

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: How to get the TOTAL length/size of an array?
by choroba (Cardinal) on Sep 22, 2023 at 06:38 UTC
    Doesn't work if the strings contain a backslash.
    @a = ('a\\b'); print length(Dumper @a)-2*@a; # 4

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      well I've said approximation and did put "work" in quotes.

      But granted I had other problems in mind than backslashes...

      edit

      also single quotes;

      DB<27> @a=("'"); DB<28> p length(Dumper @a)-2*@a 2

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

Re^2: How to get the TOTAL length/size of an array?
by bliako (Abbot) on Sep 22, 2023 at 08:19 UTC

    I totally agree with this The title of your question is misleading, because you want the total number of characters of an array of strings

    But this is misleading or could be better phrased:  The correct answer would hence be using Devel::Size to get the total memory of a deep data structure. Unless I misunderstood and by correct answer you mean to some other question!

    The (total) number of characters in a deep structure (e.g. polyglot's array of strings) has nothing to do with the size of memory it uses (as in "space in RAM" etc.) except that when one increases the other increases and vice versa. In C it is even worse because of memory alignment when storing etc.

    Consider this:

    use Devel::Size qw(size total_size); print size("A string"); 42

    bw, bliako

      > But this is misleading or could be better phrased: The correct answer would hence be using Devel::Size to get the total memory of a deep data structure. Unless I misunderstood and by correct answer you mean to some other question!

      I rephrase, the most sensible answer to a general "TOTAL length/size of an array" is memory consumption measured by Devel::Size. Anything else is a matter of interpretation and lengthy definitions in the question.

      > has nothing to do with the size of memory it uses

      well in the case of strings it's roughly proportional. Maybe mostly off by 10% in average.

      And yes, I was nitpicking... :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery