1

I wanted to make a zenity list that takes the fields for one column from one list and the fields for the other column from another list

menu=("option1" "option2" "option3")
desc=("description1" "description2" "description3")
ans=`zenity --list --column=Menu "${menu[@]}" --column=Description "${desc[@]}" --height 170`

That didn't work because it first displays all values from the first list and then from the other:

Menu Description
option1 option2
option3 description1
description2 description3

So I figured I probably need to merge them in alternating order but I don't know how.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kulpas
  • 167
  • 1
  • 9

1 Answers1

1

From man zenity :

  --column=STRING
         Set the column header

So the --column option will only set the header, and not parse your data. You will need to do some pre-processing before giving your data to zenity :

#!/usr/bin/env bash
menu=("option1" "option2" "option3")
desc=("description1" "description2" "description3")

# this for loop will create a new array ("option1" "description1" "option2" ...)
# there will be issues if the 2 arrays don't have the same length    
for (( i=0; i<${#menu[*]}; ++i)); do
    data+=( "${menu[$i]}" "${desc[$i]}" )
done

ans=$(zenity --list --column=Menu --column=Description --height 170 "${data[@]}")
Aserre
  • 4,916
  • 5
  • 33
  • 56
  • If I were to update the description array would I need to then remake the data array to reflect that? Should i just update the data array directly? – Kulpas Mar 30 '21 at 09:40
  • @Kulpas you can add new values at the end of the array by doing `data+=( "option4" "description4" )`. If you want to edit an existing value, you'll need to know its position in the array. Then, you can just do `data[1]="newDescription1"` (arrays in bash start with a 0 position) – Aserre Mar 30 '21 at 09:43