[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: efficient way to use matched string in variable substitution
From: |
Greg Wooledge |
Subject: |
Re: efficient way to use matched string in variable substitution |
Date: |
Mon, 23 Aug 2021 15:10:16 -0400 |
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 don't work.
> > for x in 0 1 2 3 4 5 6 7 8 9;do n=${n//$x/$x }; done
> > arr=($n)
That doesn't look particularly elegant to me. But we can throw it on the
pile.
unicorn:~$ f1() { local x n="$1" arr; for x in 0 1 2 3 4 5 6 7 8 9; do
n=${n//$x/$x }; done; arr=($n); }
unicorn:~$ time for ((i=1; i<=10000; i++)); do f1 682390; done
real 0.444 user 0.444 sys 0.000
> > for ((d=0; d<${#n};d+=1)); do arr+=(${n:$d:1}); done
That's the approach I'd start with, with a few minor changes.
unicorn:~$ f2() { local i n="${#1}" arr; for ((i=0; i<n; i++)); do
arr+=("${1:i:1}"); done; }
unicorn:~$ time for ((i=1; i<=10000; i++)); do f2 682390; done
real 0.413 user 0.403 sys 0.000
Slightly more efficient, it seems.
Just for fun, let's actually treat your "number N" like a number, rather
than a string.
unicorn:~$ f3() { local n="$1" tmp arr i; while ((n > 0)); do
tmp+=("$((n%10))"); ((n /= 10)); done; for ((i=${#tmp[@]}-1; i >= 0; i--)); do
arr+=("${tmp[i]}"); done; }
unicorn:~$ time for ((i=1; i<=10000; i++)); do f3 682390; done
real 0.695 user 0.691 sys 0.000
This one is less efficient, no doubt because of the extra time needed
to reverse the temporary array.
If we take steps to avoid that reversal, let's see what happens:
unicorn:~$ f4() { local n="$1" i=${#1} arr; while ((n > 0)); do
arr[--i]=$((n%10)); ((n /= 10)); done; }
unicorn:~$ time for ((i=1; i<=10000; i++)); do f4 682390; done
real 0.356 user 0.356 sys 0.000
Looks like we have a winner.
- efficient way to use matched string in variable substitution, L A Walsh, 2021/08/23
- Re: efficient way to use matched string in variable substitution,
Greg Wooledge <=
- Re: efficient way to use matched string in variable substitution, L A Walsh, 2021/08/23
- Re: efficient way to use matched string in variable substitution, Greg Wooledge, 2021/08/23
- Re: efficient way to use matched string in variable substitution, Léa Gris, 2021/08/24
- Re: efficient way to use matched string in variable substitution, Greg Wooledge, 2021/08/24
- Re: efficient way to use matched string in variable substitution, Léa Gris, 2021/08/24
- Re: efficient way to use matched string in variable substitution, Mike Jonkmans, 2021/08/24
- Re: efficient way to use matched string in variable substitution, Greg Wooledge, 2021/08/24
- Re: efficient way to use matched string in variable substitution, Mike Jonkmans, 2021/08/24
- Re: efficient way to use matched string in variable substitution, Léa Gris, 2021/08/24
- Re: efficient way to use matched string in variable substitution, Greg Wooledge, 2021/08/24