On Tue, Aug 24, 2021 at 01:31:35PM +0200, Léa Gris wrote: > Le 23/08/2021 à 21:41, L A Walsh écrivait : > > > > > > On 2021/08/23 12:10, Greg Wooledge wrote: > > > On Mon, Aug 23, 2021 at 11:36:52AM -0700, L A Walsh wrote: > > > > Starting with a number N, is there > > > > an easy way to print its digits into an array? > > n=988421 > > # Need extglob for the replacement pattern > shopt -s extglob > > # Split string characters into array > IFS=' ' read -r -a array <<<"${n//?()/ }" > > # Debug print array > declare -p array
unicorn:~$ f7() { local arr; IFS=' ' read -ra arr <<< "${1//?()/ }"; } unicorn:~$ time for ((i=1; i<=10000; i++)); do f7 682390; done real 0.393 user 0.358 sys 0.022 Interesting. I expected that one to be slower, with the overhead for the here string. You may note that this one has some "sys" time, unlike the others. E.g. unicorn:~$ f6() { local i n=${#1} arr; for ((i=0; i<n; i++)); do arr[i]="${1:i:1}"; done; } unicorn:~$ time for ((i=1; i<=10000; i++)); do f6 682390; done real 0.381 user 0.381 sys 0.000 Looks like the efficiency of "read -ra" vs. a shell loop just about makes up for the system calls used for the here string (f6 and f7 are almost tied in overall speed, with f6 just a *tiny* bit faster). Good to know.