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: Oğuz
Subject: Re: efficient way to use matched string in variable substitution
Date: Mon, 23 Aug 2021 22:13:32 +0300

23 Ağustos 2021 Pazartesi tarihinde L A Walsh <bash@tlinx.org> 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 memory serves, the ampersand character will have this function in bash
5.2.


>
> 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...
>
>
>
>

-- 
Oğuz


reply via email to

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