[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bash associative arrays... my personal trick... waht do you think?
From: |
Vito Tafuni |
Subject: |
bash associative arrays... my personal trick... waht do you think? |
Date: |
Sat, 11 Apr 2009 19:53:08 +0200 |
function idx { eval 'case $1 in '${cases[*]}' *) [ "$1" ] && { cases=(
${cases[*]} '\''"'\''$1'\''") echo '${#cases[*]}';;'\'' ); echo
'${#cases[*]}';}; esac'; }
idx all
0
idx all
0
idx jhon
1
idx all
0
as you can see the function return different values for different strings
so associative array becomes quite simple like a simple array
array[`idx name`]=value
echo ${array[`idx name`]}
value
ps this is a more accurate version that make it possible to have more than
one associative array at a time
function idx { eval 'case $1 in '$(eval echo \${${2}_CASES[*]})' *) [ "$1" ]
&& { '$2'_CASES['$(eval echo \${#${2}_CASES[*]})']='\''"'\''$1'\''") echo
'$(eval echo \${#${2}_CASES[*]})';;'\''; echo '$(eval echo
\${#${2}_CASES[*]})';}; esac'; }
array1[`idx name array1`]=value
array2[`idx name array2`]=value
--
-----------
Tafuni Vito
vitotafuni@gmail.com
---------------------------------------------
"Verba volant, scripta manent... data corrupted"
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- bash associative arrays... my personal trick... waht do you think?,
Vito Tafuni <=