Operating System - HP-UX
1819684 Members
3530 Online
109605 Solutions
New Discussion юеВ

Array restrictions with set -A

 
Chris Baugh
Occasional Advisor

Array restrictions with set -A

I've found that using set -A Arrayname, there appears to be a limit of 1024, producing the error 'ksh: 1025: subscript out of range', when > 1024 entries are encountered.

Is my assumption true, and is there any way of increasing the array subscript, or any alternative methods
4 REPLIES 4
Mark Grant
Honored Contributor

Re: Array restrictions with set -A

1024 is the maximum size of an array with the shell.

If you want more, you are either going to handle two arrays (which could get quite nasty) or start again with perl :)
Never preceed any demonstration with anything more predictive than "watch this"
Graham Cameron_1
Honored Contributor

Re: Array restrictions with set -A

From the manpage at http://docs.hp.com/hpux/onlinedocs/B2355-90680/B2355-90680.html
"
The value of all subscripts must be in the range of 0 through 1023
"
You could try another shell.
/usr/dt/bin/dtksh is pretty high-powered, although I haven't checked it out.
Awk would be ok - it's array subscripts are strings, and so can be anything.
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
A. Clay Stephenson
Acclaimed Contributor

Re: Array restrictions with set -A

1024 is the limit and, in general, when you need arrays bigger than this, the shell is going to be a dog. As has been suggested, it's time to look at awk or better still Perl. Perl actually has two types of 'arrays' -- arrays and hashes. Arrays are indexed 0-n but hashes are like awk's associative arrays so that the indices might literally be 'Red', 'Green', 'Turnips', 0, 4, ... . One more thing to note if your are using awk or Perl, the default arithmatic is floating-point by default so that if you are computing array indices 10/4 = 2.5 rather than 2 (the shell uses ONLY integer arithmatic).
If it ain't broke, I can fix that.
Ralph Grothe
Honored Contributor

Re: Array restrictions with set -A

I aggree with the others that when you have to use such big arrays you'd better use Perl for scripting.

However, though it is true that generally, if not told otherwise, Perl will do floating point arithmetics it is safeguarding against insensible input and silently corrects the programmer.

Here an example from the Perl debugger:

main::(-e:1): 0
DB<1> @a=0..9

DB<2> x @a
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
DB<3> p $a[10/4]
2
DB<4> x @a[2.9,3.1]
0 2
1 3
DB<5> p 10/4
2.5
DB<6> ;{use integer; print 10/4,"\n"}
2

DB<7>


Since fractional array indices don't make so much sense we can usually live with this.
As you can see from above you can always tell Perl to do integer arithmetics through the "use integer" pragma.
a "no integer" or end of block should return to floating point arithmetics.

A very nice feature of Perl is its "autovivification" and garbage collection.
A statement like
@a = 1000..4999
will create you a 4000 element array on the fly.
You could als use C-shell like globbing to fill e.g. an array of filenames (provided you have some hundred files ending on log)

@files = ;

The same feature works in list context on file handles if you need to read from a file handle into an array.

Besides this you can build up the most complex Lists of Lists.
Madness, thy name is system administration