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

Re: Append to array via (P) flag



On Mar 18,  8:13am, Sebastian Gniazdowski wrote:
}
} Problem is in API function that does:
} 
} __output=( "${(P@)__var_name}" "New data" )
} : ${(PA)__var_name::=${__output[@]}}

You can already do this, you just need to know the length of the
array so that you can index past the end of it:

    __output=("New Data" "Like this")
    __append="${__var_name}[${(P)#__var_name}+1]"
    : ${(PA)__append::=${(@)__output}}

With extendedglob set I'm pretty sure this will work in all cases:

    __append="${__var_name}[(r)(^*)]"
    : ${(PA)__append::=${(@)__output}}

The pattern (^*) will never match, ${(P)__append} will always be off
the end of the array, and there you go.  But that might actually be
less efficient than the extra ${(P)#__var_name}.

You can also insert stuff into the middle of an array by using a
reverse-ordered subscript, e.g. to shove the new values in between the
fifth and sixth elements:

    __insert="${__var_name}[6,5]"
    : ${(PA)__insert::=${(@)__output}}

Out of idle curiousity, why would you write a new text editor when
you can already use "vared" on a memory-mapped file?



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