Expand/Shrink

length

Definition: integer i = length(sequence s)
Description: Return the length of the sequence or string s.
The compiler issues compile-time errors/tests for run-time errors, if s is/might be an atom or integer (see technicalia).
If s is a string, the result is the number of bytes, see technicalia for utf8/16 handling.
If s is a sequence, the result is the number of top-level elements.
  (In the latter, nested sub-sequences, however complicated, and string elements, each count as one towards the length.)

Alternatively (when constant ORAC is 1 in p.exw, which it is in all released versions), ~s is shorthand for length(s).
Examples:
?length("")                             -- 0: the empty string
?length({})                             -- 0: the empty sequence
?length({{}})                           -- 1: s[1] is {}.
?length("four")                         -- 4: 4 bytes/characters
?length({1.2,"three",{4,{5,"six"}},7})  -- 4: s[1] is 1.2
                                        --    s[2] is "three",
                                        --    s[3] is {4,{5,"six"}},
                                        --    s[4] is 7.
Implementation: See :%opLen in builtins\VM\pLen.e and the technicalia section below.
See Also: sequence, string javascript.length
Expand/Shrink