Operating System - HP-UX
1820594 Members
1649 Online
109626 Solutions
New Discussion юеВ

2 dimensional Array in Shell script

 
SOLVED
Go to solution
Chris Fung
Frequent Advisor

2 dimensional Array in Shell script

Hi all,

I have a question about using the 2 dimensional array in Shell script? Can anyone tell me how to implement the 2 dimensional array through Ksh or sh ?

Much appreciated if you can provide additional link for reference.

Many thanks,

Chris,
4 REPLIES 4
John Poff
Honored Contributor
Solution

Re: 2 dimensional Array in Shell script

Hi Chris,

I don't think you can do it in either ksh or in the Posix shell. Here is a URL to the HP-UX 11.00 commands docs for 'sh-posix' and 'ksh'. The snippet of text is found in the descriptions for both 'ksh' and 'sh-posix':

http://docs.hp.com/hpux/onlinedocs/B2355-90680/B2355-90680.html

"The shell supports a limited one-dimensional array facility. An element of an array parameter is referenced by a subscript. A subscript is denoted by a [, followed by an arithmetic expression, followed by a ]. See the "Arithmetic Evaluation" subsection. To assign values to an array, use set -A name value .... The value of all subscripts must be in the range of 0 through 1023. Arrays need not be declared. Any reference to a named parameter with a valid subscript is legal and an array is created if necessary. Referencing an array parameter without a subscript is equivalent to referencing the first element."


Now, someone might know of some trick to access a two dimensional array in a shell script, but I don't know of any way to do it. I think the question has been posed here on the forum before, but I couldn't find it anywhere.

I think you can do two dimensional arrays in 'awk', but I haven't tried it. You can do it in Perl, which is what I would suggest, especially since it is so easy to get Perl for HP-UX and install it.

I'm just curious, but what problem are you trying to solve with the two dimensional arrays?

JP
harry d brown jr
Honored Contributor

Re: 2 dimensional Array in Shell script


perl

perl will run many times faster than anything you can implement in sh/ksh. And yes, it has arrays!

If you must continue with "two" dimensional array's in sh/ksh, then you will have to use two different variable names and just fake it!


live free or die
harry
Live Free or Die
Steve Faidley
Valued Contributor

Re: 2 dimensional Array in Shell script

If Perl is not a viable solution for you give us more detail of what exactly your trying to accomplish and we may be able to help you.
If it ain't broke, let me have a look at it.
Steve Faidley
Valued Contributor

Re: 2 dimensional Array in Shell script

If Perl is not a viable solution for you give us more detail of what exactly your trying to accomplish and we may be able to help you.
If it ain't broke, let me have a look at it.