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

Re: more splitting




On 2026-04-14 20:26, Bart Schaefer wrote:
If you redo 'hex' to accept the name of the variable instead of the
expansion of the variable, you can fix all of this:

function hex ()
{
    local _hex_var element
    echo
    if [[ -p /dev/fd/0 ]]; then
       read -d '' -r _hex_var
    else
       _hex_var=$1
    fi
    set -- ${(P)_hex_var}
    print -rC1 -- ${(q+)${(Q)@}}
    echo "\n-----------------------------\n"
    for element ("$@") print -rn -- $element | od -vAx -tx1 -tc
}

hex var
print var | hex
Perfect!  If the *name* is passed, then it doesn't matter if it's a data stream.  I trust a name is always a string, so front door or back door ... all the same.  I hope my comments are correct:

# Test variable:
var=("a b" c$'\n''d e f'' ''g h' ij)

# % hex var
# % print var | hex
# We pass the NAME of the array variable thus the pipe || argument methods work exactly the same. If we passed the value of the array instead, the pipe would lose all splitting information, turning it into a string. Yes? Bart knows these things.
function hex ()
{
    local _hex_var element
    echo
    if [[ -p /dev/fd/0 ]]; then  # We are smoking a pipe.
       read -d '' -r _hex_var   # -d:terminate on nul, -r:'raw'--all literal.
    else
       _hex_var=$1             # We receive the NAME of the var :-) tx. Bart
    fi
    set -- ${(P)_hex_var}        # Expand PPs to value of named var.
    # (q+):quote the quotes ... sorta, -C1: columns split on spaces.
    print -rC1 -- ${(q+)@}
    echo "\n-----------------------------\n"
    for element ("$@") print -rn -- $element | od -vAx -tx1 -tc
}

... but many questions running the thing ...

On Tue, Apr 14, 2026 at 8:26 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
    print -rC1 -- ${(q+)${(Q)@}}
Sorry, there's an extra (Q) in there, should be just
    print -rC1 -- ${(q+)@}

Too much copy-pasting.

That was one of my questions :-)
Already fixed above.







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