Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

KSH - Array error: subscript out of range 1

Status
Not open for further replies.

pmcmicha

Technical User
May 25, 2000
353
I am getting this error when attempting to assign an array to some database queries.

set -A DBRS `<DB SYNTAX>`

I have 13 queries running and 2 of those queries returns this error. The first one has some 7146 entries while the second one has a whole lot more. Short of having to rewrite this script in perl, does anyone have any ideas how to avoid this error or fix it. Upgrading to KSH93 is not an option at this point. But if that is fix, please let me know.

KSH VERSION: 88

Thanks in advance.
 
you might be hitting a limit of the array size for ksh.
not sure if the limitation is OS-based OR ksh-version based though.

Is awk-ing the output of the command an option? I guess that depends on the rest of the logic of your script.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Yes. I can use awk, nawk, or gawk for the operation instead. Though my skills with that syntax is still limited...do you have a suggestion?
 
well..... depending on the rest of the script and on what you actually do with DBRS... you options might differ.

What/how do you use the DBRS? Do you have a loop of some kind or what?

post a piece your code dealing with DBRS after you set - someone might be able to help you out.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Here is a snip of the code:

============================================================
#!/usr/bin/ksh -p

DISP() {
print &quot;${1}&quot;
print ${DBRS[@]}|gawk '{gsub(&quot; &quot;,&quot;\n\t\t&quot;);print &quot;\t\t&quot; $0}'
}

# Run Queries.
set -A DBRS `<DB SYNTAX>`
DISP &quot;SOME DESCRIPTIVE SENTENCE HERE&quot;
<REPEAT 13 TIMES>
============================================================

Since the database query returns the result like so:
(Ex: 100 101 102 103 104....)
I use the gsub to turn that space into a newline and then add two tabs to the end of it as well for the log file. The statement has an extra &quot;\t\t&quot; after print to account for the first element in the array in since it does not have a space in front of it and it only applies itself to that element.

OUTPUT:
============================================================
SOME DESCRIPTIVE SENTENCE HERE
100
101
102
103
...
============================================================
 
#!/usr/bin/ksh -p

DISP() {
print &quot;${1}&quot;
gawk '{gsub(&quot; &quot;,&quot;\n\t\t&quot;);print &quot;\t\t&quot; $0}'
}

# Run Queries.
`<DB SYNTAX>` | DISP &quot;SOME DESCRIPTIVE SENTENCE HERE&quot;
<REPEAT 13 TIMES>


Jean Pierre.
 
Sorry, don't need `

#!/usr/bin/ksh -p

DISP() {
print &quot;${1}&quot;
gawk '{gsub(&quot; &quot;,&quot;\n\t\t&quot;);print &quot;\t\t&quot; $0}'
}

# Run Queries.
<DB SYNTAX> | DISP &quot;SOME DESCRIPTIVE SENTENCE HERE&quot;


Jean Pierre.
 
How about something like that ......

DISP() # $1 - query to run
{
eval &quot;${1}&quot; |gawk '{gsub(&quot; &quot;,&quot;\n\t\t&quot;);print &quot;\t\t&quot; $0}'
}

# Run Queries.
DISP &quot;<DB SYNTAX - query1>&quot;
DISP &quot;<DB SYNTAX - query2>&quot;
DISP &quot;<DB SYNTAX - query3>&quot;

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
aigles,
Thanks, that solution works great.


Vlad,
Your solution isn't working out as well. After the DB query is executed, it isn't liking the | to another process after being a referenced variable. It would pipe normally to another process if being run directly. Not sure what the changes with that since the syntax doesn't change and if run with eval &quot;${1}&quot; it works fine. Tried it without the eval, but then the query didn't even run.


The other thing that I would like to do is get a total count of the query. Before that wouldn't have been a problem since I could have used ${#DBRS[@]} syntax, but now wouldn't know what to use.
 
Try this:

[tt]
DISP() {
print &quot;${1}&quot;
gawk '
{
sub(&quot;^ *&quot;,&quot;&quot;);
sub(&quot; *$&quot;,&quot;&quot;);
gsub(&quot; +&quot;,&quot; &quot;);
count += gsub(&quot; &quot;,&quot;\n\t\t&quot;) + 1;
print &quot;\t\t&quot; $0;
}
END {
print &quot;\nRows count:&quot;,count;
}
'
}

<DB QUERY> | DISP &quot;Some descriptive sentence here&quot;
[/tt]

If you are sure that there no leading or trailing or multiple spaces within the result of tjhe query, you can remove the first three sub/gsub statements.

Jean Pierre.
 
aigles and Vlad,

Thanks so much for you help, this is just what I need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top