jqlang/jq

$s[1] versus $s[1:2] in strings

fadado opened this issue · 3 comments

Strings do not allow indexed access: why $s[1] is not valid and equal to $s[1:2] in strings?

I insist. This code is generic for strings and arrays in jq:

def rotate($n):
    .[$n:] + .[:$n]
;

because slices syntax is identical for strings and arrays, but unlike Python

>>> s="lala"
>>> s[0]
'l'

jq does not allow indexed acces to one character in strings:

$ jq -n '"lala"|.[0]'
jq: error (at <unknown>): Cannot index string with number

You're right, it ought to work.

EDIT: Hmmm, maybe not. There's no character type. Consider what .[0] means when . is an array... the output will be the value at the zeroth index, not an array of that. But if . is a string... then the output would have to be a character, which would be... a number!

What I'd like to do is add support for binary data, where binary data is represented as an array of integers 0..255 (naturally). Internally this would be efficient, but from a jq program's point of view it would be indistinguishable from an array of numbers (mostly; .[0] = 256 should probably elicit an error, for example). Once we have binary data support, we'd really want indexing to be similar for arrays and strings, but again, the output of .[<index>] for a string input would have to be a codepoint numeric value, NOT a string.