in reply to How to get the TOTAL length/size of an array?
say length @many_strings; # 1
I think it should be pointed out that in this statement, length puts @many_strings into scalar context, thereby yielding 6 (as shown explicitly in the following line), and then 1 is returned because the length of the string “6” happens to be 1. With warnings enabled, this gives a warning, as it should:
17:09 >perl -wE "my @many_strings = ('abc', 'cd', 'e', 'fg', 'hi', 'he +llo world'); say length @many_strings;" length() used on @many_strings (did you mean "scalar(@many_strings)"?) + at -e line 1. 1 17:29 >
For the record, in Raku swl’s use of reduce could be written:
use v6d; my Str @many-strings = |<abc cd e fg hi>, 'hello world'; @many-strings.raku.put; @many-strings.elems.put; say [+] @many-strings.map: { .chars };
Output:
17:09 >raku 2090_SoPW.raku Array[Str].new("abc", "cd", "e", "fg", "hi", "hello world") 6 21 17:09 >
using Raku’s built-in reduction metaoperator [+], documented here.
Hope that’s of interest,
Athanasius <°(((>< contra mundum | סתם עוד האקר של פרל, |
|
---|