add printDB to doc, adjust writeDB and readDB examples

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2020-05-15 00:21:40 +02:00
parent d1f509a00f
commit 23a3d4b8f4
1 changed files with 40 additions and 19 deletions

View File

@ -457,7 +457,7 @@ Usually message is automatically forwarded in 'commands.sh', but you can forwar
*usage:* send_interactive "${CHAT[ID]}" "message" *usage:* send_interactive "${CHAT[ID]}" "message"
*replaces:*' incproc *replaces:* incproc
---- ----
@ -537,6 +537,28 @@ cat "${DATADIR:-}/myvalues.jssh"
["value2"] "a value" ["value2"] "a value"
["whynot","subindex2","text"] "This is an example content for pseudo multidimensional bash array" ["whynot","subindex2","text"] "This is an example content for pseudo multidimensional bash array"
["whynot","subindex2"] "whynot B" ["whynot","subindex2"] "whynot B"
["whynot","subindex1"] "whynot A"
```
##### jssh_printDB
Print content of an ARRAY to STDOUT. ARRAY name must be delared with "declare -A ARRAY" before calling printDB..
*usage:* jssh_printDB "ARRAY"
*example:*
```bash
# Prepare array to store vaules
declare -A PRINTVALUES
# read file data-bot-bash/myvalues.jssh into array READVALUES
jssh_readDB "PRINTVALUES" "${DATADIR:-}/myvalues"
# print DB to stdout
jssh_printDB READVALUES
["value1"] "example"
["value2"] "a value"
["whynot","subindex2","text"] "This is an example content for pseudo multidimensional bash array"
["whynot","subindex2"] "whynot B"
["whynot","subindex1"] "whynot A"``` ["whynot","subindex1"] "whynot A"```
``` ```
@ -612,17 +634,15 @@ jssh_readDB "READVALUES" "${DATADIR:-}/myvalues"
# sinple command to output values ONLY # sinple command to output values ONLY
printf "${READVALUES[*]}" printf "${READVALUES[*]}"
example a value This is an example content for pseudo multidimensional bash array whynot B whynot A
# print DB to stdout
# function to output key and value jssh_printDB READVALUES
printarr() { declare -n __p="$1"; for k in "${!__p[@]}"; do printf "%s=%s\n" "$k" "${__p[$k]}" ; done ; } ["value1"] "example"
["value2"] "a value"
printarr READVALUES ["whynot","subindex2","text"] "This is an example content for pseudo multidimensional bash array"
value1=example ["whynot","subindex2"] "whynot B"
value2=a value ["whynot","subindex1"] "whynot A"
whynot,subindex2,text=This is an example content for pseudo multidimensional bash array
whynot,subindex2=whynot B
whynot,subindex1=whynot A
# access Arrray # access Arrray
@ -639,13 +659,14 @@ READVALUES["value3"]="new value"
READVALUES[whynot,subindex3]="new subindex value" READVALUES[whynot,subindex3]="new subindex value"
# new output # new output
printarr READVALUES jssh_printDB READVALUES
value1=example ["value1"] "example"
value2=this is a changed value ["value3"] "new value"
whynot,subindex2,text=This is an example content for pseudo multidimensional bash array ["value2"] "this is a changed value"
whynot,subindex3=new subindex value ["whynot","subindex2","text"] "This is an example content for pseudo multidimensional bash array"
whynot,subindex2=whynot B ["whynot","subindex3"] "new subindex value"
whynot,subindex1=whynot A ["whynot","subindex2"] "whynot B"
["whynot","subindex1"] "whynot A"
``` ```
https://linuxhint.com/associative_array_bash/ https://linuxhint.com/associative_array_bash/
@ -935,5 +956,5 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca
#### [Prev Best Practice](5_practice.md) #### [Prev Best Practice](5_practice.md)
#### [Next Notes for Developers](7_develop.md) #### [Next Notes for Developers](7_develop.md)
#### $$VERSION$$ v0.94-pre-7-g64efe96 #### $$VERSION$$ v0.94-pre-9-gd1f509a