Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Function

len — the type-dependent length of a value

Synopsis

len(val: array|bytes|ip|map|net|null|record|set|string|type) -> int64

Description

The len function returns the length of its argument val. The semantics of this length depend on the value’s type.

For values of each of the supported types listed below, len describes the contents of val as indicated.

TypeWhat len Returns
arrayElements present
bytesCount of 8-bit bytes
ipBytes in the address (4 for IPv4, 16 for IPv6)
mapKey-value pairs present
netBytes in the prefix and subnet mask (8 for IPv4, 32 for IPv6)
null0
recordFields present
setElements present
stringCount of unicode code points

For values of the type type, len describes the underlying type definition of val as indicated below.

Category of type ValueWhat len Returns
arraylen of the defined element type
enumCount of defined symbols
errorlen of the type of the defined wrapped values
maplen of the defined value type
primitive1
recordCount of defined fields
setlen of the defined element type
unionCount of defined member types

Examples


The length of values of various types

# spq
values {this,kind:kind(this),type:typeof(this),len:len(this)}
# input
[1,2,3]
0x0102ffee
192.168.4.1
2001:0db8:85a3:0000:0000:8a2e:0370:7334
|{"APPL":145.03,"GOOG":87.07}|
192.168.4.0/24
2001:db8:abcd::/64
null
{a:1,b:2}
|["x","y","z"]|
"hello"
# expected output
{that:[1,2,3],kind:"array",type:<[int64]>,len:3}
{that:0x0102ffee,kind:"primitive",type:<bytes>,len:4}
{that:192.168.4.1,kind:"primitive",type:<ip>,len:4}
{that:2001:db8:85a3::8a2e:370:7334,kind:"primitive",type:<ip>,len:16}
{that:|{"APPL":145.03,"GOOG":87.07}|,kind:"map",type:<|{string:float64}|>,len:2}
{that:192.168.4.0/24,kind:"primitive",type:<net>,len:8}
{that:2001:db8:abcd::/64,kind:"primitive",type:<net>,len:32}
{that:null,kind:"primitive",type:<null>,len:0}
{that:{a:1,b:2},kind:"record",type:<{a:int64,b:int64}>,len:2}
{that:|["x","y","z"]|,kind:"set",type:<|[string]|>,len:3}
{that:"hello",kind:"primitive",type:<string>,len:5}

The length of various values of type type

# spq
values {this,kind:kind(this),type:typeof(this),len:len(this)}
# input
<[string]>
<[{a:int64,b:string,c:bool}]>
<enum(HEADS,TAILS)>
<error(string)>
<error({ts:time,msg:string})>
<|{string:float64}|>
<|{string:{x:int64,y:float64}}|>
<int8>
<{a:int64,b:string,c:bool}>
<|[string]|>
<|[{a:int64,b:string,c:bool}]|>
<(int64|float64|string)>
# expected output
{that:<[string]>,kind:"array",type:<type>,len:1}
{that:<[{a:int64,b:string,c:bool}]>,kind:"array",type:<type>,len:3}
{that:<enum(HEADS,TAILS)>,kind:"enum",type:<type>,len:2}
{that:<error(string)>,kind:"error",type:<type>,len:1}
{that:<error({ts:time,msg:string})>,kind:"error",type:<type>,len:2}
{that:<|{string:float64}|>,kind:"map",type:<type>,len:1}
{that:<|{string:{x:int64,y:float64}}|>,kind:"map",type:<type>,len:2}
{that:<int8>,kind:"primitive",type:<type>,len:1}
{that:<{a:int64,b:string,c:bool}>,kind:"record",type:<type>,len:3}
{that:<|[string]|>,kind:"set",type:<type>,len:1}
{that:<|[{a:int64,b:string,c:bool}]|>,kind:"set",type:<type>,len:3}
{that:<int64|float64|string>,kind:"union",type:<type>,len:3}

Unsupported values produce errors

# spq
values {this,kind:kind(this),type:typeof(this),len:len(this)}
# input
true
10m30s
error("hello")
1
2024-07-30T20:05:15.118252Z
# expected output
{that:true,kind:"primitive",type:<bool>,len:error({message:"len: bad type",on:true})}
{that:10m30s,kind:"primitive",type:<duration>,len:error({message:"len: bad type",on:10m30s})}
{that:error("hello"),kind:"error",type:<error(string)>,len:error({message:"len()",on:error("hello")})}
{that:1,kind:"primitive",type:<int64>,len:error({message:"len: bad type",on:1})}
{that:2024-07-30T20:05:15.118252Z,kind:"primitive",type:<time>,len:error({message:"len: bad type",on:2024-07-30T20:05:15.118252Z})}