Re: Defect in manual section "Conditional Constructs" / case

2021-08-24 Thread Dale R. Worley
"Dietmar P. Schindler" writes: > Doesn't the example I gave above show that quotes are removed? If they > weren't, how could word aa with pattern a""a constitute a match? As you say, >a""a< matches as a case-pattern when the case word is >aa<. But that's not due to quote removal, because what th

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: Defect in manual section "Conditional Constructs" / case

2021-08-24 Thread Lawrence Velázquez
On Tue, Aug 24, 2021, at 4:44 PM, Dietmar P. Schindler wrote: > Doesn't the example I gave above show that quotes are removed? If they > weren't, how could word aa with pattern a""a constitute a match? The quotes are handled by the matching process itself, *not* as part of the usual shell expansio

Re: Defect in manual section "Conditional Constructs" / case

2021-08-24 Thread Dietmar P. Schindler
- Original Message - From: "Andreas Schwab" To: Cc: Sent: Tuesday, August 24, 2021 3:19 PM Subject: Re: Defect in manual section "Conditional Constructs" / case On Aug 24 2021, dietmar_schind...@web.de wrote: In the section https://www.gnu.org/software/bash/manual/bash.html#Conditi

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: Defect in manual section "Conditional Constructs" / case

2021-08-24 Thread Andreas Schwab
On Aug 24 2021, dietmar_schind...@web.de wrote: > In the section > https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs > in the description of the "case" command there is no mention (as far as I > can see, it doesn't follow from the documented expansions etc.) that a > _patte

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

Defect in manual section "Conditional Constructs" / case

2021-08-24 Thread Dietmar_Schindler
(resending this from private account without the silly company "Confidelity note") In the section https://www.gnu.org/software/bash/manual/bash.html#Conditional-Constructs in the description of the "case" command there is no mention (as far as I can see, it doesn't follow from the documented e

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