Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to get the TOTAL length/size of an array?

by swl (Parson)
on Sep 21, 2023 at 05:02 UTC ( #11154562=note: print w/replies, xml ) Need Help??


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

my $len; $len += length $_ for @many_strings; say $len;
or:
use List::Util qw/sum/; say sum map {length $_} @many_strings;

Edit: Or, as documented in List::Util:

use List::Util qw /reduce/; say reduce { $a + length $b } 0, @many_strings;

Replies are listed 'Best First'.
Re^2: How to get the TOTAL length/size of an array? (List Utils Modules)
by eyepopslikeamosquito (Archbishop) on Sep 21, 2023 at 08:18 UTC

    use List::Util qw /reduce/; say reduce { $a + length $b } 0, @many_strings;

    For cheap thrills, I browsed List::MoreUtils but nothing jumped out at me. This one is similar to yours (both programs produce 21):

    use v5.38; use List::MoreUtils qw/reduce_0/; my @many_strings = ("abc", "cd", "e", "fg", "hi", "hello world"); say reduce_0 { $a + length $b } @many_strings;

    I'm always out-classed by tybalt89 in these kinds of challenges, so I look forward to seeing what concoctions he comes up with when the USA time zone kicks in. :)

    List Utils Modules

    Updated: added "List Utils Modules" section

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11154562]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (9)
As of 2023-12-11 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?