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

Re: Can't call multi-arg math function from array subscript



On Sun, Apr 2, 2017 at 8:51 PM, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> Single-argument functions are OK:
>
> % string='abcde'
> % onearg() { return $1 }
> % functions -M onearg
> % print ${string[1,onearg(3)]
> abc
>
> Add another argument and strange things start happening:
>
> % twoarg() { return $(( $2 - $1 )) }
> % functions -M twoarg
> % print ${string[1,twoarg(1,4)]}
> twoarg: bad math expression: operand expected at end of string
> twoarg: bad math expression: operand expected at end of string
>
> Why two error messages?
>
> I assume the comma is messing things up; let's try it like this:
>
> % print ${string[1,$(( twoarg(1,4) ))]}
> zsh: parse error
> zsh: parse error
>
> Just to confirm:
>
> % print ${string[1,$(( onearg(4) ))]}
> abcd

I guess this basically confirms that the comma messes up parsing:

% func='twoarg(1,4)'
% print ${string[1,func]}
abc


-- 
Mikael Magnusson



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