text list to 2d array and pick subarray item in bash

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
blumenwesen
Posts: 38
Joined: Sun Apr 10, 2022 10:02 pm

text list to 2d array and pick subarray item in bash

Post by blumenwesen »

I have a text list that is divided into 2D arrays and I want to use zenity to pick out a subarray.

TEXT

Code: Select all

"words1 string0" "words1 string1" "words1 string3"\n"words2 string0" "words2 string1" "words2 string3" : "words2 string4" "words2 string5" "words2 string6" : "words2 string7" "words2 string8" "words2 string9"\n"words3 string0" "words3 string1" "words3 string3" : "words3 string4" "words3 string5" "words3 string6"

The arrays are separated by line breaks, the subarrays are separated by colons.

2D

Code: Select all

val[0,0]="words1 string0" "words1 string1" "words1 string3"
val[1,0]="words2 string0" "words2 string1" "words2 string3"
val[1,1]="words2 string4" "words2 string5" "words2 string6"
val[1,2]="words2 string7" "words2 string8" "words2 string9"
val[2,0]="words3 string4" "words3 string5" "words3 string6"

MY cryptic code to convert text to 2d ;)

Code: Select all

z=$(cat "FILE"); 
isz=$IFS; IFS=[$'\x0A']; z=($z); 
IFS=$isz; 
for (( y=0; y<${#z[@]}; y++ )); do 
    x="${z[$y]//[ ][‡][ ]/$'\x0A'}"; 
    IFS=[$'\x0A'] read -d "" -ra x <<< "$x"; 
    for (( w=0; w<${#x[@]}; w++ )); do 
        ar0["$y,$w"]="${x[$w]}"; 
    done; 
done
echo "${ar0[0,2]}"

zenity array = 1-3 und subarrays = (array1 = 1, array2 = 3, array3 = 2)

Since array1 only has 1 subarray with 3 items, only 1 should be displayed.
Array2 has 3 subarrays with 9 items, so 3 should be displayed.
Array3 with 2 subarrays and 6 items, 2 should be displayed.

Zenity should be adjusted dynamically so that array1 does not display 3 subarrays as in this example.

Code: Select all

z=$(zenity --forms --title="TITLE" --add-combo="" --combo-values="1|2|3" --add-combo="" --combo-values="1|2|3"); echo "$z"
Post Reply

Return to “Programming”