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

Re: subscript not working as "I" expected it would



On Sat, Nov 4, 2023, at 4:48 AM, Roman Perepelitsa wrote:
>> $(list) expands to an array. ${$(list)[N]} gives the Nth element of the array.
>>
>> % print -r -- ${$(print foo bar)[1]}
>> foo
>> % print -r -- ${$(print foo bar)[2]}
>> bar
>>
>> $(list) can be an array with just one element but it's still an array.
>> This is the case in your code snippet.
>>
>> "$(list)" on the other hand is a scalar. ${"$(list)"[N]} gives the Nth
>> character.
>>
>>     % print -r -- ${"$(print foo bar)"[1]}
>>     f
>>     % print -r -- ${"$(print foo bar)"[2]}
>>     o

Also note that subscripting occurs *before* (s) is applied, so your
(s.: .) is not doing what you think it is doing.

	% printf '<%s>' ${(s.: .)$(echo a b: c d)[2]}; echo
	<b:>
	% printf '<%s>' ${${(s.: .)"$(echo a b: c d)"}[2]}; echo
	<c d>

-- 
vq




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