Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: more splitting




On 2026-04-17 09:25, Mark J. Reed wrote:
... You do have the option of keeping the delimiters around, but they show up as separate elements in between the others in the result array. That's mostly useful when you have a selection of possible delimiters and need to know which one you got at each position; less so when you know that there had to have been a newline there because it was the only thing you were splitting on.
Yeah, there could be different ways of doing it.  As I said, the essential thing is that I finally 99% understand what's going on and I can cope with it.  I haven't had a problem I couldn't deal with yet, it's just been so hard to figure out what's going on. Zsh does not lend itself to self-study, one must be informed of these deep things.
Zsh's slightly-older sister Bash has a built-in function called readarray that splits input on a delimiter and puts the resulting strings into an array; it is unusual in that it does keep the delimiter around by default at the end of each element. 
That's about exactly what I was  suggesting in my private msg. just now.  Splitting information could be preserved very easily.  But altering the data ... wrong on principle.

But I very rarely see code using it that way; most of the time it's called with the -t option to disable that behavior and leave the delimiters out.
Good for piping tho? Like Philippe's:

 while read -u 0 -k 1 c; do var+=$c; done

... with a lot lest trouble.





Messages sorted by: Reverse Date, Date, Thread, Author