1822556 Members
3062 Online
109642 Solutions
New Discussion юеВ

Re: typeset -A

 
SOLVED
Go to solution
Jun Zhang_4
Regular Advisor

typeset -A

Several docs noted that

typeset -A foo
foo = ([z]=a [y]=b)

is for ksh to use to assign associative array. I tried on hpux and got
ksh: typeset: bad option(s)
on linux, it says
ksh: typeset -A: unknown option.

What's wrong?

Jun Z
Food lover
9 REPLIES 9
A. Clay Stephenson
Acclaimed Contributor

Re: typeset -A

I think that you are confusing typeset -A with set -A --- and the arrays are not associative but numeric ranging from 0 to a maximum of 1023.


For example,
set -A arry red orange yellow green blue indigo violet

echo ${arry[0]}
$ red
echo ${arry[6]}
$ violet
If it ain't broke, I can fix that.
Jeff Schussele
Honored Contributor

Re: typeset -A

Hi Jun,

In ksh & sh-posix you'd use
set -A array_name value1 value2 ....
to set a one-dimensional array.
That's all the shell can handle AFAIK.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Michael Tully
Honored Contributor

Re: typeset -A

You might try 'set -A' which sets values to an array.

Anyone for a Mutiny ?
Elena Leontieva
Esteemed Contributor

Re: typeset -A

You can also create a one-dimensional array in Korn shell as follows:

array[0]=tom
array[1]=jane
print ${array[0]}
tom
print ${array[1]}
jane

Elena
curt larson_1
Honored Contributor
Solution

Re: typeset -A

the -A attribute to define an associative array is available only on version of ksh newer the the 11/16/88 version. Unfortunately, HP's /usr/bin/ksh isn't.

if it did you'd do something like this:
typeset -A color
color[apple]=red
color[banana]=yellow
...etc.
Jun Zhang_4
Regular Advisor

Re: typeset -A

The associative array is what I'm trying to use.
What happens to you when you type in ksh

typeset -A foo

?
Jun
Food lover
Jeff Schussele
Honored Contributor

Re: typeset -A

Hi Jun Z

Simply

/usr/bin/ksh typeset: bad option(s)

set is your huckleberry....

jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Jeff Schussele
Honored Contributor

Re: typeset -A

Excuse me, Did we give you BAD advice........?
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
curt larson_1
Honored Contributor

Re: typeset -A

in hp's /usr/bin/ksh if you want to assign values to an array the syntax would be:
set [+-]A arrayName args

the values are assigned sequentially from zero and has a maximum of 1024 elements, 1023 is the largest index.

use -A to unset the array prior to the assignment.

#assigns f[0]=1 and f[1]=4
set -A f 1 4
#assigns f[0]=5, but f[1] is left alone
set +A f 5

if you really need to use associative arrays you'll have to use a tool that supports them,
/usr/dt/bin/dtksh, awk, perl, etc.