I'm a newbie.....multidimensional arrays in shell scripts is possible???
If so, can anyone guide me with an example.....and also can anyone tell me, how we can create a table like ouput format in shell script....the output should look like this one:
1 2 3 4 5 6
6 5 4 3 2 1
1 2 3 1 2 1
2 2 3 3 4 5
7 8 9 0 4 2
matrix/table with rows and columns kind of output is that possible......kindly explain me with some examples....
Depends on what "shell". Even in ancient of days Bourne shell (something you'll still find on many Solaris hosts), you can always think of an array as:
varname_${index1}_${index2}
If you just had to. In other words, you can use variable values to create unique variable names... and thus get an array effect.
bash and ksh have arrays, new editions of korn shell (and bash as well) support single dimension indexed as well as associative arrays. You can exploit associative arrays to do quasi-multi dimensional array indexing using a similar technique to what I described for bourne shell... e.g varname["${index1} ${index2}"]
So assignment isn't necessarily hard in the bourne shell case... but evaluating requires an extra level of evaluation to be performed, so you have to do some extra escaping.
Korn/bash won't have this extra eval requirement. So you can just do (in bash):
declare -A varname
index1=1
index2=2
varname["${index1} ${index2}"]='my value'
val=${varname["${index1} ${index2}"]}
(I realize I'm not being consistent... the underscore is a requirement in the bourne shell case, since it's just named variables really, and I'm using space as the separator between the indices in the korn/bash associative array case)