1748052 Members
4817 Online
108758 Solutions
New Discussion юеВ

Re: Korn shell question

 
SOLVED
Go to solution
Quark
Valued Contributor

Korn shell question

Hi,

I wanted to write a ksh script that uses arrays.
Since I was not familiar with the syntax, I looked up some code on the net.
This is what I found:
"
typeset -A wavelength


wavelength["red"]=650
wavelength["orange"]=590
wavelength["green"]=510
wavelength["blue"]=475
wavelength["indigo"]=445
wavelength["violet"]=400
"

This is what I want to do but when test this code then 'typeset -A' is not recognised.
If replace 'typeset -A' by 'set -A' then I get 'red: bad number'

It seems that version of Korn shell on my system is to old (not Korn 93). I am running 11.11.

Is this correct or is the syntax just wrong?

Any help much appreciated.

KRis
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: Korn shell question

Hi Kris:

The standard HP-UX version of the Korn shell found in '/usr/bin/ksh' is Korn88. You can find the Korn93 version as:

/usr/dt/bin/dtksh

Regards!

...JRF...

Hein van den Heuvel
Honored Contributor

Re: Korn shell question

KRis,

It may well be that an Ksh script with arrays is the optimal (learning, processing, price, security, tools) way to solve the challenge on hand.

However, in my experience most such task are better done in AWK or PERL.

What problem are you really trying to solve?
Outline it here (with concrete input and output) and we may be able to help you better.

Regards,
Hein.
Oviwan
Honored Contributor

Re: Korn shell question

Hey

with set -A you can only use integer as index of the array. check man ksh

Regards
Steven Schweda
Honored Contributor

Re: Korn shell question

And what happened to "yellow"?
Ralph Grothe
Honored Contributor

Re: Korn shell question

From your sample snippet,
what you want is a hash
(or a so called associative array).
I am not into Korn shell in particular
because I never require it.
As for most array capable shells,
they expect indeces to be unsigned integers
(often only ranging from 0-1023).
But you can script in awk, Perl, Python or Ruby for key value paired lists.
Madness, thy name is system administration
Zeev Schultz
Honored Contributor

Re: Korn shell question

no "typeset -A" in hpux ksh. use "set -A name variable" instead.

Moreover,I'm convinced that ksh array is a standard 1-level array with index. I.e you should use : wavelength[1]="red",[2]="orange" and so on. What you may need here is a named reference. It is nicely implemented with perl's hashes. ksh93 gives associative arrays,as well,there you can use strings as an index.
So computers don't think yet. At least not chess computers. - Seymour Cray
James R. Ferguson
Acclaimed Contributor

Re: Korn shell question

Hi (again):

While I agree that associative arrays are better handled in 'awk' or Perl (where they are called "hashes"), you _CAN_ do this with the Korn93 shell _ON_ HP-UX with as I noted above. You must use :

/usr/bin/dt/dtksh

Regards!

...JRF...