How i create a mulidimensional array ?

How i create a mulidimensional array ?

am 24.01.2008 14:42:06 von Santana

Hei all,
there is possible in UNIX sheell Script (Korne Shell) create a
mulidimensional array ? How ?

I want something simmilar like this C declaration :

int mat[2][2];
mat[0][0]=1;
mat[0][1]=1;
mat[1][0]=2;
mat[1][1]=2;



Help me please !


Regards,
Paulito

Re: How i create a mulidimensional array ?

am 24.01.2008 15:21:56 von Stephane CHAZELAS

On Thu, 24 Jan 2008 05:42:06 -0800 (PST), Santana wrote:
[...]
> there is possible in UNIX sheell Script (Korne Shell) create a
> mulidimensional array ? How ?
>
> I want something simmilar like this C declaration :
>
> int mat[2][2];
> mat[0][0]=1;
> mat[0][1]=1;
> mat[1][0]=2;
> mat[1][1]=2;
[...]

Why would you need that in a shell script?!

You can do

mat_0_0=1
mat_0_1=1

But if you tell us what you want to do in the end, chances are
that we'll be able to point you do a more shell-like way to do
it.

If you want to do some programming as opposed to shell
scripting, I'd suggest you consider programming languages like
perl, awk or ruby (note that interpreters for those languages
can be called from a shell script).

Having said that, recent versions of ksh93 have multidimensional
array support, for what it's worth.

--
Stephane

Re: How i create a mulidimensional array ?

am 24.01.2008 15:32:23 von Ed Morton

On 1/24/2008 7:42 AM, Santana wrote:
> Hei all,
> there is possible in UNIX sheell Script (Korne Shell) create a
> mulidimensional array ? How ?
>
> I want something simmilar like this C declaration :
>
> int mat[2][2];
> mat[0][0]=1;
> mat[0][1]=1;
> mat[1][0]=2;
> mat[1][1]=2;

If you're using 2-D arrays you're probably doing something that's inappropriate
for shell itself. Instead, use one of the tools you invoke from shell, e.g.:

$ awk 'BEGIN{
mat[0,0]=1
mat[0,1]=1
mat[1,0]=2
mat[1,1]=2

for (i=0;i<=1;i++)
for (j=0;j<=1;j++)
printf "mat[%d,%d]=%d\n",i,j,mat[i,j]

exit
}'
mat[0,0]=1
mat[0,1]=1
mat[1,0]=2
mat[1,1]=2

Note that by convention awk arrays with numeric-string indices normally start at
1 rather than zero, but you can start them at zero or anything else if you choose.

Regards,

Ed.

Re: How i create a mulidimensional array ?

am 25.01.2008 01:33:38 von brian_hiles

Santana wrote:
> Is it possible in UNIX shell (Korn Shell) to create a
> multidimensional array? How?
> ...
> mat[0][0]=1;

First of all, I'm assuming that you are using ksh version 1988,
not version 1993, which has associative arrays, which can be
utilized to easily implement pseudo multi-dimension arrays, in
the method that n/awk(1) uses to do the same -- look up keyword
"SUBSEP" in the awk(1) manpage.

Actually, there are no arrays per se in kornshell of any version.
All "arrays" are actually hashes, with optimizations for integral
indices.

I point out that virtually all versions of ksh(1) versions 1988
or newer have a compile-time option to implement multi-dimensional
arrays -- you should at least try it. IIRC, the syntax is:

mat[0,0]=1

Failing that, I recommend either rewriting your algorithm, or
utilizing John H. DuBois III's function library AssocArr to
emulate multi-dimensional arrays in kornshell:

ftp://ftp.armory.com/pub/lib/ksh/AssocArr

=Brian