01-06-2005 01:32 AM
I want to build a two-dimensional array with columns and rows like a spreadsheet and have the rightmost column be a total of each row, and the bottom row be a total of the entire column above it:
.......OS: AIX HP-UX Linux Total
Grp: A 5 10 2 17
B 0 3 9 12
-- -- -- -- --
Tot 5 13 11 29
I have never worked with two-dimensional arrays in ksh. Can you do that?
typeset A [10,10]
01-06-2005 02:09 AM
In your example-
ncols=10 ; # specifiy number of columns
p1=2 ; p2=3 ; # Desired position
# Convert position p1,p2 to offset in "A"
-- Rod Hills
(PS - another tool like "perl" might be a better way to go...)
01-06-2005 02:13 AM
Neiter A[2,3] or A is valid shell syntax.
Probably the most straightforward approach would be awk --- but it only supports single-dimensional arrays as well BUT awks arrays will work. How can that be? Awk's array indices are associative rather that purely numeric like those of the shell.
This means that X[tom],X[harry], ... are perfectly valid so X[2,3] works as well because "2,3" is treated as a string.
01-07-2005 01:00 AM
Example, with an array a[6,6] and the two indices being i,j ....
The single array a[x] would yield:
x = (i-1) * 6 + j
The only caviat is that the upper limit of one of the indices must be known or arbitrarily set, in this case 6.