help-bash
[Top][All Lists]
Advanced

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

Re: String to array


From: Alex fxmbsw7 Ratchev
Subject: Re: String to array
Date: Thu, 30 Sep 2021 19:31:26 +0200

i was thinking not to loop, for speed
but no speed ok ok , =)

On Thu, Sep 30, 2021, 19:07 Greg Wooledge <greg@wooledge.org> wrote:

> On Thu, Sep 30, 2021 at 06:51:50PM +0200, Alex fxmbsw7 Ratchev wrote:
> > i dunno but i did string=hello and a time while i 10k and 1k, in 3s
> neither
> > finished
> > hm may be proot cellfone slowness
>
> This thread is not about speed.  The OP wanted to experiment with
> different solutions that are explicitly *not* optimal, for fun.
>
> If you want a fast solution, just use a loop:
>
> unset array
> n=${#string}
> for ((i=0; i<n; i++)); do array[i]=${string:i:1}; done
>
> unicorn:~$ unset array; n=${#string}; for ((i=0; i<n; i++)); do
> array[i]=${string:i:1}; done
> unicorn:~$ declare -p array
> declare -a array=([0]="h" [1]="i" [2]=" " [3]="b" [4]="y" [5]="e"
> [6]=$'\t' [7]="w" [8]="h" [9]="y" [10]=$'\n' [11]=$'\E' [12]="[" [13]=";"
> [14]="1")
>
>


reply via email to

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