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

Accessing elements in list by index and range #99

Closed
achempion opened this issue Oct 15, 2021 · 5 comments
Closed

Accessing elements in list by index and range #99

achempion opened this issue Oct 15, 2021 · 5 comments
Labels
functions syntax template language syntax

Comments

@achempion
Copy link
Contributor

achempion commented Oct 15, 2021

You can have this data structure in json

{"test": [1, 2, 3]}

In order to have it formatted like 1, 2 and 3 you have to use something like this

{%= join(test[0..count(test)], ", ") %} and {%= test[count(test) - 1], ", ") %}
// or like this
{%= join(test[0..-2], ", ") %} and {%= test[-1], ", ") %}

It whould be great to have [] operator for accessing elements and count and count them.

@erdos
Copy link
Owner

erdos commented Oct 15, 2021

Hello, this functionality is already available in the latest stencil:

@erdos erdos added the invalid This doesn't seem right label Oct 15, 2021
@achempion
Copy link
Contributor Author

I think [] operator works only on objects to access properties, not arrays, i.e. I can't slice arrays or get n-th element.

@erdos erdos removed the invalid This doesn't seem right label Oct 15, 2021
@erdos
Copy link
Owner

erdos commented Oct 15, 2021

you are right, I need to check it again. Let me get back to this question at the end of the day.

@erdostw
Copy link
Collaborator

erdostw commented Oct 15, 2021

I have release version 0.3.28 with the following improvements:

  1. Fixed the array access operator to work on lists (it was working only for Clojure vectors, not for Java lists). The bug also affected the map function for nested lists.
  2. Added the joinAnd function that supports your use case!

Let me know your thoughts.

@achempion
Copy link
Contributor Author

achempion commented Oct 20, 2021

It's working, would be great to have range access, like a[0..-1] but joinAnd solves currently all the needs 💚

@erdostw erdostw added syntax template language syntax functions labels Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functions syntax template language syntax
Projects
None yet
Development

No branches or pull requests

3 participants