Operating System - HP-UX
1834406 Members
2432 Online
110067 Solutions
New Discussion

ksh set -A negative value problem

 
SOLVED
Go to solution
Muthukumar_5
Honored Contributor

ksh set -A negative value problem

Hai All,

How can we use negative values on ksh shell arrays.

When I tried to use as,

TEST="-1 -2 -3"
set -A ARR $TEST
ksh: set: -1: unknown option

But it is working for,

TEST="1 -2 -3"
set -A ARR $TEST
echo ${ARR[0]} ${ARR[1]} ${ARR[2]}

But on sh, bash it will not be problem there.

Share you knowledge.

Thanks,
Muthu
Easy to suggest when don't know about the problem!
7 REPLIES 7
Mauro Gatti
Valued Contributor

Re: ksh set -A negative value problem

Reading man ksh it seems to be this limit:

"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."



RGDS
Ubi maior, minor cessat!
Muthukumar_5
Honored Contributor

Re: ksh set -A negative value problem

Gatti,

that was good reference note.

But ksh shell array is supporting second, n index of array's with negative values there.?

Can we use any other method to index array values with negative values there.?

Thanks for your time.
Easy to suggest when don't know about the problem!
Michael Schulte zur Sur
Honored Contributor

Re: ksh set -A negative value problem

Hi,

have you tried TEST="\\-1 -2 -3"
?

greetings,

Michael
Michael Schulte zur Sur
Honored Contributor
Solution

Re: ksh set -A negative value problem

Hi,

my last answer did now work.
But this does it.
TEST="-- -1 -2 -3"
-- does signify end of options.

greetings,

Michael

Mauro Gatti
Valued Contributor

Re: ksh set -A negative value problem

It is so strange...
I show my tries...
In effect:
$ ksh
$ unset PLUTO
$ set -A PLUTO "1" "2" "3"
$ echo ${PLUTO[0]} " " ${PLUTO[1]} " " ${PLUTO[2]}
1 2 3
$ unset PLUTO
$ set -A PLUTO "1" "-2" "3"
$ echo ${PLUTO[0]} " " ${PLUTO[1]} " " ${PLUTO[2]}
1 -2 3
$ unset PLUTO
$ set -A PLUTO "-1" "2" "3"
ksh: -1: bad option(s)

So it seems only the first value don't have a minus sign.

But look this strange behavior:

$ unset PLUTO
$ typeset -i PLUTO[0]=1
$ typeset -i PLUTO[1]=1
$ typeset -i PLUTO[2]=1
$ echo ${PLUTO[0]} " " ${PLUTO[1]} " " ${PLUTO[2]}
1 1 1
$ PLUTO[0]=${PLUTO[0]}-1
$ echo ${PLUTO[0]} " " ${PLUTO[1]} " " ${PLUTO[2]}
0 1 1
$ PLUTO[0]=${PLUTO[0]}-1
$ echo ${PLUTO[0]} " " ${PLUTO[1]} " " ${PLUTO[2]}
4294967295 1 1

So I think, when you try to assing
set -A PLUTO "-1" "2" "3"
or

set -A PLUTO "1" "-2" "3"
you assign a string value and not a numeric value.

Ubi maior, minor cessat!
Mauro Gatti
Valued Contributor

Re: ksh set -A negative value problem

Using Michael method it seems to work fine:
$ unset PLUTO
$ set -A PLUTO "--" "-1" "2" "3"
$ echo $(( ${PLUTO[0]} - 1))
-2


:-)
Ubi maior, minor cessat!
Muthukumar_5
Honored Contributor

Re: ksh set -A negative value problem

Thanks micheal. It is working.
Easy to suggest when don't know about the problem!