Re: more splitting
Ray Andrews <[email protected]> Wed, 15 Apr 2026 14:17:48 -0700
| Newsgroups | gmane.comp.shells.zsh.devel,gmane.comp.shells.zsh.user |
|---|---|
| Message-ID | <[email protected]> |
Thanks all. Very nice explanation Dana. I'm 90% there.
> No, actually, it can't have been designed differently. Pipes (and all
file descriptors) are an operating system construct, they don't know
what's on either end, they just move bytes.
I mean that arrays might have been designed to carry a separator
character between elements. ASCII FF maybe. So when piped,
reconstructing an array would be easier. Mind, this is the shallowest
of speculations, there could be very good reasons why that's not done.
I'm not whining. Anyway the point is that I now understand that
information IS thrown away -- splitting information is lost, not by
virtue of some magic characters being removed, but by virtue of C level
storage information being lost. The socks don't know what drawer they
came from. I get it.
% var=("a b" c$'\n''d e f'' ''g h' ij)
% hex var
'a b'
$'c\nd e f g h'
ij
-----------------------------
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
000000 69 6a
i j
000002
... The space after the 'b' is gone forever -- it has been taken as an
instruction to split, NOT as a literal char. The ' $'\n' ' construction
has been taken as an instruction to add ASCII char 0a. The space beween
'f' and 'g' is quoted therefore a printed character, not an instruction
to split. The next space is unquoted therefore an instruction to split,
and it's gone.
% print -n $var
a b c
d e f g h ij# # What's the hash for?
% hex var
'a b'
$'c\nd e f g h'
ij
...
... 'a b' is displayed quoted because of the space -- the ticks aren't
'real' . Thus 'ij' needs no ticks.
2 /aWorking/Zsh/Source/Wk 6 % var2=( $=var ); hex var2
a
b
c
d
e
f
g
h
ij
... as expected, except that the newline disappeared.
% var2=( ${(f)var} ); hex var2
'a b c'
'd e f g h ij'
-----------------------------
000000 61 20 62 20 63
a b c
000005
000000 64 20 65 20 66 20 67 20 68 20 69 6a
d e f g h i j
00000c
... love it. Split on newline. All spaces preserved. One thing tho:
when $var is created, would not the splitting spaces vanish? But we see
the space between 'b' and 'c' remains in var2. Likewise the space
between 'h' and 'i' -- how are they retained? Or are they put back for
display?
I'm not at the summit, but I'm at the South Col.
% typeset -p var
typeset -a var=( 'a b' $'c \n d e f g h' ij ) # stretch out for comp.
% var= ( "a b" c$'\n''d e f'' ''g h' ij ) # stretched ...
... so typeset output very close to original variable creation
keystrokes. Better! Running output of typeset -p to create a new array,
it's identical to the original. :-) Typeset edits my input string
while retaining the product exactly. Love it. IOW, typeset returns
what I should have typed in the first place. That explains the movement
of the dollar too, which has always baffled me.
95%
So a mad scientist could take the output from typeset -p, pipe that
string and use it to create a new var downstream of the pipe!