Elvish: Problem with array slicing syntax

Created on 5 May 2018  Â·  6Comments  Â·  Source: elves/elvish

Can't use variable names as the starting index of the slice when using a slice of a list. The shell interprets it as if it would be a namespace:

i = 2
put [ a b c d e ][$i:5]

It should output â–¶ [c d e], but it gives a compilation error that variable 5 is not found in the $i namespace.
What do you think about using .. (used in many languages e.g. Ruby, Crystal, Perl6) instead of : for slices?

Most helpful comment

Aha. Good point. Let's use ...

All 6 comments

You can wrap the variable in curly braces, like this:

put [ a b c d e ][{$i}:5]
â–¶ [c d e]

@zzamboni Another solution is [ a b c d e ][(put $i):5]. But both are ugly and hard to read.

Aha. Good point. Let's use ...

Planning to address this before 0.15.

If someone were to tackle this is the intent is to support : and .. with : marked as deprecated in the documentation?

Yes, and use of : for slices should emit a warning.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zethra picture zethra  Â·  3Comments

krader1961 picture krader1961  Â·  6Comments

xiaq picture xiaq  Â·  4Comments

edouard-lopez picture edouard-lopez  Â·  7Comments

krader1961 picture krader1961  Â·  4Comments