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

Re: mysterious newline



That code doesn't append any newlines. It builds an array of lines. All the final2+=" " code does is insert an entry into the array that's nothing but a space. The space isn't needed; it could just be the empty string. Some later code must be printing out the final2 array one entry per line.

On Thu, Nov 19, 2026 at 5:42 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
This reads the lines in an array and if the first words on each line are not identical a blank line in inserted otherwise the lines are tight:

local prev="${(M)final[1]#*$' '}"
local final2=()
for ((ii=1; ii<=$#final; ii++)); do
    ffile="${(M)final[ii]#*$' '}"
    if [[ "$prev" == "$ffile" ]]; then
        final2+=$final[ii]
    else
        final2+=" "
        prev=$ffile
    fi    
done

... it works but I don't understand why.  >final2+=" "< creates a newline, which is what I want but it would seem to only append a space, not a newline.  Perhaps since final2 is an array, zsh cleverly presumes that any '+=' must mean 'append to next element'?  I figger so but I'd better ask.  IOW, if I wanted to append a space to the end of the last element, I'd have to be explicit about that, yes?  It seems reasonable.





--
Mark J. Reed <markjreed@xxxxxxxxx>


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