To complement the already complete answer for others (and future self ), the withIndex method comes from Groovy, documentation here Iterable (Groovy JDK enhancements) . Basically, the use is withIndex(offset) on an interable, and returns (value, index) in order, with index starting at offset.
withIndex() is documented here in the Nextflow standard library. You may find equivalent methods in the Groovy docs, but Groovy methods and types are not guaranteed to be supported by Nextflow, so make sure to consult the Nextflow docs first.
One caveat to Phil’s answer: if you want to track the index alongside each channel value, you should apply withIndex() at a point where the ordering is consistent. Doing collect() -> withIndex() on a channel will give you a different ordering on every run. So in your example, you would need to find something upstream of words that is ordered (e.g. a samplesheet that you just loaded as a list of rows) and add the index there.
Ha! Thanks Ben for pointing to this page, much appreciated!
A note here: this is the first time I encounter this documentation page, despite spending quite some time searching. I kindly suggest some SEO, as an example, this page does not appear (at least on my setup) when googling nextflow "withIndex".
Thanks for the precisions. For my use case (disambiguating outputs), I don’t need to enforce ordering, but the precision could indeed be important for others.