bug-bash
[Top][All Lists]
Advanced

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

efficient way to use matched string in variable substitution


From: L A Walsh
Subject: efficient way to use matched string in variable substitution
Date: Mon, 23 Aug 2021 11:36:52 -0700
User-agent: Thunderbird 2.0.0.24 (Windows/20100228)

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:

arr=(${N//[0-9]/\1 })
 or
arr=(${N//[0-9]/$1 })

Instead of using loops (my=declare):

 n=988421
 for x in 0 1 2 3 4 5 6 7 8 9;do n=${n//$x/$x }; done
 arr=($n)
 my -p arr
declare -a arr=([0]="9" [1]="8" [2]="8" [3]="4" [4]="2" [5]="1")

or w/substrings:

 for ((d=0; d<${#n};d+=1)); do arr+=(${n:$d:1}); done
 my -p arr
declare -a arr=([0]="9" [1]="8" [2]="8" [3]="4" [4]="2" [5]="1")

Not a big thing, but having some way for the match of an RE
to be specified in the output would be handy...





reply via email to

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