bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: efficient way to use matched string in variable substitution


From: Mike Jonkmans
Subject: Re: efficient way to use matched string in variable substitution
Date: Tue, 24 Aug 2021 16:50:05 +0200

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 very useful there; but I don't want to be wrongly credited for
> this smart implementation.

Oh no, it is not my idea.
It came from that same thread.
Just wanted to tell that it is the fastest method, on my pc at least.

> Made it into a fancy utility function:
> 
> 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 -n arr="$2"
>   # shellcheck disable=SC2034 # shellcheck broken nameref usage check
>   arr=("${BASH_REMATCH[@]:1}")
> }

shellcheck is very useful (in vim),
But it does not seem to be moving on :(

Regards, Mike Jonkmans



reply via email to

[Prev in Thread] Current Thread [Next in Thread]