At present, I’m struggling to find solution to either of the following problems:
Create an assoc array from the result of mysql sql query. I normally create an indexed array from a mysql sql query result as below:
mapfile -t a_dummy <<< "$(mysql -u root –disable-column-names –silent -B -e "select * from dummy_tbl;" "$DB_NAME")"
where $DB_NAME is the variable pointing to DB name string.
Here's one way, using sed. Note that this will only work, however, if none of the elements of the original array contain whitespace.
declare -A "newArray=( $(echo ${oldArray[@]} | sed 's/[^ ]*/[&]=&/g') )"
The sed command takes each array element 'x' and replaces it with the string '[x]=x', suitable for an associative array assignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With