Duplicating an array 1:1 in zsh -


although seems simple, there small nuance obvious solution.

the following code cover situations:

arr_1=(1 2 3 4) arr_2=($arr_1) 

however, empty strings not copy over. following code:

arr_1=('' '' 3 4) arr_2=($arr_1) print -l \   "array 1 size: $#arr_1" \   "array 2 size: $#arr_2" 

will yield:

array 1 size: 4 array 2 size: 2 

how go getting true copy of array?

it "array subscripts" issue, specify array subscript form select elements of array ($arr_1 instance) within double quotes:

arr_1=('' '' 3 4) arr_2=("${arr_1[@]}") #=> arr_2=("" "" "3" "4") 

each elements of $arr_1 surrounded double quotes if empty.

a subscript of form ‘[*]’ or ‘[@]’ evaluates elements of array; there no difference between 2 except when appear within double quotes.
‘"$foo[*]"’ evaluates ‘"$foo1 $foo[2] ..."’, whereas ‘"$foo[@]"’ evaluates ‘"$foo1" "$foo[2]" ...’.
...
when array parameter referenced ‘$name’ (with no subscript) evaluates ‘$name[*]’,

-- array subscripts, zshparam(1)

and empty elements of arrays removed according "empty argument removal", so

arr_2=($arr_1) #=> arr_2=(${arr_1[*]}) #=> arr_2=(3 4) 

above behavior not in case.

24. empty argument removal
if substitution not appear in double quotes, resulting zero-length argument, whether scalar or element of array, elided list of arguments inserted command line.

-- empty argument removal, rules expansion zshexpn(1)


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -