Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fadado opened this issue Oct 28, 2017 · 3 comments
Open

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

fadado opened this issue Oct 28, 2017 · 3 comments

Comments

@fadado
Copy link

fadado commented Oct 28, 2017

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

@fadado
Copy link
Author

fadado commented Nov 6, 2017

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

@nicowilliams
Copy link
Contributor

nicowilliams commented Nov 28, 2017

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!

@nicowilliams nicowilliams removed the bug label Nov 28, 2017
@nicowilliams
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants