I have in Bash (>=4.2) a peculiar scenario (data structure) that can't be changed. It's like this (not sure if this is called associative array or multidimensional array):
#!/bin/bash
declare -gA arrdata
arrdata["jimmy","vacation invoices"]="69008981"
arrdata["jimmy","budget"]="12345678 00392212"
arrdata["mike","budget"]="63859112 98005342 66675431"
arrdata["mike","extra"]="23332587"
#!/bin/bash
function check_if_expense_exist() {
#bash code array loops to return 0 if the type exist for the given name, 1 if not exist
}
#Expected results
#Given "jimmy" as first argument and "vacation invoices" as second should return 0
#Given "mike" as first argument and "vacation invoices" as second should return 1
#!/bin/bash
function check_if_data_exist() {
#bash code array loops to return 0 if data exist for the given name and type of expense, 1 if not exist
}
#Expected results
#Given "jimmy" as first argument and "vacation invoices" as second, and "11111121" as third should return 1 because doesn't exist
#Given "mike" as first argument and "budget" as second, and "98005342" as third should return 0 because it exists
These functions can be very short:
$ check_if_expense_exist() { [[ -n "${arrdata["$1","$2"]:+not set}" ]]; }
$ check_if_expense_exist jimmy budget && echo y || echo n
y
$ check_if_expense_exist jimmy budgit && echo y || echo n
n
$ check_if_data_exist() { [[ "${arrdata["$1","$2"]}" =~ (^| )"$3"( |$) ]]; }
$ check_if_data_exist jimmy budget 12345678 && echo y || echo n
y
$ check_if_data_exist jimmy budget 1234568 && echo y || echo n
n