sum#
Add up every value in the list and return the numerical total.
Σύνοψη#
my $total = sum @values;
my $total = sum 1..10; # 55
Τι επιστρέφεται#
A single number - an integer if every argument summed as an integer, a float otherwise. Integer arithmetic wraps on overflow rather than promoting, matching upstream. For an empty list, sum returns undef for backwards compatibility; if that is awkward use sum0 instead.
Παραδείγματα#
my $n = sum 3, 9, 12; # 24
my $n = sum @bar, @baz; # concatenated lists
my $n = sum(); # undef
my $n = sum map { length } @s; # total length of all strings
Οριακές περιπτώσεις#
Empty list returns
undef. Usesum0for a0default.Mixing ints and floats promotes the running total to a float.
Non-numeric strings are coerced via the usual numification.
Διαφορές από το upstream#
Fully compatible with upstream.
Δείτε επίσης#
sum0- same assum, but returns0for an empty list.product- multiplies rather than adds.reduce- the generic reduction whensumis not specific enough.