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

Re: more splitting





On 2026-04-14 16:24, Philippe Altherr wrote:
What you try to do is pretty much impossible because when you do 'print -rn $var',you turn the strings in '$var' into a single one where the strings from '$var' are separated by spaces. How is 'hex' supposed to know which of the spaces in that string were from the strings in '$var' and which ones were introduced to separate the strings in '$var'?!?
I quite understand that the trick is to keep the structure of the data intact.  It's precisely all this kind of thing that ties me in knots.

Below is a version where both, 'hex $var' and 'print -rN $var | hex', produce the same result. It expects the stdin to contain strings separated by NUL characters. That's why the print call has to use the -N option to join its arguments with NUL characters instead of spaces.
Your version seems good but I'm still trying to understand exactly what's going on.  Split on nuls you say.  Here's the output:

% var=("a b" c$'\n''d e f'' ''g h')

% print -rN $var | hex

'a b'
$'c\nd e f g h'

-----------------------------

000000  61  20  62
         a       b
000003
000000  63  0a  64  20  65  20  66  20  67  20  68
         c  \n   d       e       f       g       h
00000b

... exactly the same as the 'argument method'.  But, if it's split on nuls, where are they? Should I not see some '00' in the output? I'm hoping this hex viewer will let me see all splitting, be it by spaces or newlines or nuls. Next time I have a splitting headache, I'll be able to view the data in question with absolute clarity. Or ... have you cleverly managed to add them and then remove them, leaving the data exactly as found?

BTW, the pipe thing might be better abandoned, not really needed, but I thought it was worth a look.








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