Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Koichi Murase
Fun to see different techniques from different people. I have also played with this interesting problem. In my environment, the following implementations seem to be the fastest. f21b() { local -a "arr=('\${1:'{0..$((${#1}-1))}':1}')"; arr=("${arr[@]@P}"); } # Any strings f31() { local arr i=${#1}

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread L A Walsh
On 2021/08/24 05:06, Greg Wooledge wrote: 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. If you set your TIMEFORM

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Greg Wooledge
On Tue, Aug 24, 2021 at 04:16:46PM +0200, Léa Gris wrote: > string2array() { > # Splits the string's characters into the array > # $1: The input string > # $2: The output array name > [[ "$1" =~ ${1//?/(.)} ]] > # shellcheck disable=SC2178 # shellcheck broken nameref type check > local

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Mike Jonkmans
On Tue, Aug 24, 2021 at 04:16:46PM +0200, Léa Gris wrote: > Le 24/08/2021 à 15:09, Mike Jonkmans écrivait : > > This seems to be the fastest: > > f12 () { [[ "$1" =~ ${1//?/(.)} ]]; local arr=( "${BASH_REMATCH[@]:1}" ); } > > Awesome Mike, would you like to add this answer to SO? > > It would be

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Mike Jonkmans
On Tue, Aug 24, 2021 at 09:24:35AM -0400, Greg Wooledge wrote: > On Tue, Aug 24, 2021 at 03:09:55PM +0200, Mike Jonkmans wrote: > > This seems to be the fastest: > > f12 () { [[ "$1" =~ ${1//?/(.)} ]]; local arr=( "${BASH_REMATCH[@]:1}" ); } > > time for ((i=1; i<=1; i++)); do f0 682390; done >

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Léa Gris
Le 24/08/2021 à 15:09, Mike Jonkmans écrivait : This seems to be the fastest: f12 () { [[ "$1" =~ ${1//?/(.)} ]]; local arr=( "${BASH_REMATCH[@]:1}" ); } time for ((i=1; i<=1; i++)); do f0 682390; done real0m0,296s user0m0,296s sys 0m0,000s Awesome Mike, would you like to add th

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Greg Wooledge
On Tue, Aug 24, 2021 at 03:09:55PM +0200, Mike Jonkmans wrote: > This seems to be the fastest: > f12 () { [[ "$1" =~ ${1//?/(.)} ]]; local arr=( "${BASH_REMATCH[@]:1}" ); } > time for ((i=1; i<=1; i++)); do f0 682390; done > real0m0,296s > user0m0,296s > sys 0m0,000s Your CPU is a

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Mike Jonkmans
On Tue, Aug 24, 2021 at 02:15:46PM +0200, Léa Gris wrote: > Le 24/08/2021 à 14:06, Greg Wooledge écrivait : > > > unicorn:~$ f6() { local i n=${#1} arr; for ((i=0; i > arr[i]="${1:i:1}"; done; } > > unicorn:~$ time for ((i=1; i<=1; i++)); do f6 682390; done > > real 0.381 user 0.381 sys 0.00

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Léa Gris
Le 24/08/2021 à 14:06, Greg Wooledge écrivait : unicorn:~$ f6() { local i n=${#1} arr; for ((i=0; i See my featured version to also capture space and newlines: https://stackoverflow.com/a/68907322/7939871 -- Léa Gris

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Greg Wooledge
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 it

Re: efficient way to use matched string in variable substitution

2021-08-24 Thread Léa Gris
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 ex

Re: efficient way to use matched string in variable substitution

2021-08-23 Thread Greg Wooledge
On Mon, Aug 23, 2021 at 12:41:58PM -0700, L A Walsh wrote: > > > 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? > > > > "Easy"? Or "efficient"?

Re: efficient way to use matched string in variable substitution

2021-08-23 Thread L A Walsh
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? "Easy"? Or "efficient"? Your subject header says one, but your body says the other. Efficient,

Re: efficient way to use matched string in variable substitution

2021-08-23 Thread Chet Ramey
On 8/23/21 3:13 PM, Oğuz wrote: > 23 Ağustos 2021 Pazartesi tarihinde L A Walsh yazdı: > >> Starting with a number N, is there >> an easy way to print its digits into an array? >> I came up with a few ways, but thought this >> would be nice (with '\1' or '$1' being what was matched >> in the 1st

Re: efficient way to use matched string in variable substitution

2021-08-23 Thread Oğuz
23 Ağustos 2021 Pazartesi tarihinde L A Walsh yazdı: > Starting with a number N, is there > an easy way to print its digits into an array? > I came up with a few ways, but thought this > would be nice (with '\1' or '$1' being what was matched > in the 1st part), this could be statement: If memo

Re: efficient way to use matched string in variable substitution

2021-08-23 Thread Greg Wooledge
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? "Easy"? Or "efficient"? Your subject header says one, but your body says the other. > arr=(${N//[0-9]/\1 }) > arr=(${N//[0-9]/$1 }) Obviously those