help-bash
[Top][All Lists]
Advanced

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

Re: String to array


From: Greg Wooledge
Subject: Re: String to array
Date: Thu, 30 Sep 2021 10:39:36 -0400

On Thu, Sep 30, 2021 at 05:25:35PM +0300, Tapani Tarvainen wrote:
> On Thu, Sep 30, 2021 at 09:48:13AM -0400, Greg Wooledge (greg@wooledge.org) 
> wrote:
> 
> > As I understand it, you're asking for "no explicit loops", and "no
> > forked processes".
> 
> Yes (although I'd be interested in pretty solutions with forked
> processes, too).

Oh, that opens up a *world*.

... a world of bugs, as it turns out.

unicorn:~$ mapfile -t -d $'\xff' array < <(printf %s "$string" | sed 
$'s/./&\xff/g')
unicorn:~$ declare -p array
declare -a array=([0]=$'h\377' [1]=$'i\377' [2]=$' \377' [3]=$'b\377' 
[4]=$'y\377' [5]=$'e\377' [6]=$'\t\377' [7]=$'w\377' [8]=$'h\377' [9]=$'y\377' 
[10]=$'\n\E\377' [11]=$'[\377' [12]=$';\377' [13]=$'1\377')

Why isn't it removing the delimiter, when the delimiter is $'\xff'?  It
works(*) with other custom delimiters:

unicorn:~$ mapfile -t -d q array < <(printf %s "$string" | sed $'s/./&q/g')
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\E' [11]="[" [12]=";" [13]="1")

Is it something to do with \xff not being a valid "character" in UTF-8
or something?  Um....

unicorn:~$ LC_ALL=C mapfile -t -d $'\xff' array < <(printf %s "$string" | sed 
$'s/./&\xff/g')
unicorn:~$ declare -p array
declare -a array=([0]=$'h\377' [1]=$'i\377' [2]=$' \377' [3]=$'b\377' 
[4]=$'y\377' [5]=$'e\377' [6]=$'\t\377' [7]=$'w\377' [8]=$'h\377' [9]=$'y\377' 
[10]=$'\n\E\377' [11]=$'[\377' [12]=$';\377' [13]=$'1\377')

Try again?

unicorn:~$ LC_ALL=C
unicorn:~$ mapfile -t -d $'\xff' array < <(printf %s "$string" | sed 
$'s/./&\xff/g')
unicorn:~$ unset LC_ALL
unicorn:~$ declare -p array
declare -a array=([0]=$'h\377' [1]=$'i\377' [2]=$' \377' [3]=$'b\377' 
[4]=$'y\377' [5]=$'e\377' [6]=$'\t\377' [7]=$'w\377' [8]=$'h\377' [9]=$'y\377' 
[10]=$'\n\E\377' [11]=$'[\377' [12]=$';\377' [13]=$'1\377')

OK, I officially Do Not Understand.

(*) Element 10 $'\n\E' is wrong, so it didn't actually work.  I suppose
    I need to write some messages for the other list now.



reply via email to

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