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

Re: Splitting into a one element array



14.09.2015, 23:07, "Jesper Nygårds" <jesper.nygards@xxxxxxxxx>:
> I am writing a function where I want to split the $LBUFFER on whitespace,
> and then handle the last element in the resulting array. My initial attempt
> looked like this:
>
> local dir=${${(z)LBUFFER}[-1]}
>
> This works if $LBUFFER contains more than one word, but fails if it is one
> word only. I tested the behavior with the following function:
>
> trysplit() {
>     local astring="aaa bbb"
>     print -l ${=astring}
>     print
>     print -l ${${=astring}[-1]}
>     print
>     local cstring="ccc"
>     print -l ${=cstring}
>     print
>     print -l ${${=cstring}[-1]}
> }
>
> with this result:
>
> % trysplit
> aaa
> bbb
>
> bbb
>
> ccc
>
> c
>
> So, my interpretation of the above is that IF the split results in only one
> word, the result is not handled as an array with one element, but as a
> regular string. And then only the first letter of the $cstring is printed.
>
> Is there some way to handle this that I have missed, or do I need to check
> the split result for size, and then treat the one-element case differently?

I have a workaround for this:

    local -a arr
    arr=( ${(z)LBUFFER} )
    local dir=${arr[-1]}



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