OpenRefine / OpenRefine/OpenRefine

Allow sub-array ranges to end with 0 in order to include the last element

Open
#5,361 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Type: Feature Request
Dominant language
Java
Stars
12k
Forks
2.2k
Avg merge
1d 14h
Merged PRs (30d)
27

Description

GREL provides an elegant way to extract a sub-array from an array using two indices, for example value[1,3], as shown in the documentation. Also very convenient is the ability to specify the second value as negative in order to count from the end, such as "internationalization"[1,-2] (also from the documentation). As it stands, however, this second style can't be used to extract a sub-array that includes the last element (i.e. one can't specify the last n elements) - for that you need to revert to using slice, or (in the case of a string) substring (or one could make a cumbersome use of the length function).

While these other functions will do the job well enough, I think it makes sense to simply allow 0 to be used as the second value in the range. For example "ABCD"[-2,0] to give "CD". I imagine a counter-argument might be that this would be the only case where that second value is not an actual array element (or rather, 0 could appear to refer to the first element) but since the element represented by that second value is always excluded from the subset (e.g. [1,3] does not include the [3] element), currently one can never sensibly use 0 there anyway - that is, there is no sub-array range, other than a trivial empty array, which can be described with [n,0]. To put it another way, I'm proposing that the 'count from end' logic, when used for the end of a range specification, should apply to values less than 1 rather than just negative values.

Although, as I mentioned, we're not lacking in alternatives at the moment, I keep finding myself having to turn to the documentation to remind myself of the right syntax for slice just to get around this. So I think the proposed feature would make this sub-array mechanism more elegant while still being logical.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the GREL implementation for two-index range expressions and the tests covering positive and negative range endpoints. Verify the current behavior for examples such as "ABCD"[-2,0], then add coverage showing that an endpoint of 0 includes the final element while existing range behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.