KSH: Split String into smaller substrings based on count

KSH
HP-SOL-Lin
Cannot use xAWK

I have several strings that are quite long and i want to break them down into smaller substrings.

What I have

String = "word1 word2 word3 word4 .....wordx"

What I want

String1="word1 word2"
String2="word 3 word4"
String3="word4 word5"
Stringx="wordx wordx+1"
etc.....

How Can i break this up to where if my string is longer than x words, break into smaller strings no longer than x?

Some strings are longer than 2000 words, so i cannot use a shell array as there is a max of 1024 fields.

ideas?

If it's too large for a shell variable, too large for an array, to the point that you're trying to split it between multiple separate variables, that's a subtle hint, written in mile-high flashing neon letters, that it's a bad idea to try and load enormous amounts of text into a shell variable. You should be trying to make a loop of some sort, handle it piecemeal, instead of loading it whole.

Where did all that text come from? Are you loading it from file, or could you load it from file? What does or would the file look like?

Its not too big for a shell variable....Its in a shell variable.

I just want it smaller so i can put it into an array.

I can't give you code that I know for a fact will work given the text is so huge that it's too many elements for an array.

If it's large to the point that you're trying to cram things in two-by-two into multiple shell variables to make it fit, it's a subtle hint, written in mile-high flashing neon letters, that it's a bad idea to try and load enormous amounts of text into a shell variable. You should approach the problem from another direction instead of trying to load it whole. That's how shell code usually works. It also makes for code that works in other shells, not just yours. Someone who wrote this for BASH would not have your problem and might not imagine it could ever cause you trouble...

So again, why do you want it in an array? What are you actually trying to do? Not the way you're hellbent on doing so -- the actual goal of having the data in an array.

does it matter why I want to put 2000 words into array? NO
The fact is I have a string that i need to put into an array and its too big, so I want to split it up. PERIOD.

If you dont want to help, then dont reply.

Your programming habits are going to paint you into a corner someday, but I'll try and give you what you want.

It won't be elegant because it can't be. I still think you're going about this the wrong way.

N=0

set --

echo "$ENORMOUSSTRING" | tr -s ' ' '\n' | while read STRING
do
        set -- $1 "$@"

        if [ "$#" -gt 1 ]
        then
                read `print "VARNAME%04d" $N` <<EOF
$1 $2
EOF
                shift 2
                N=$((N+1))
        fi
done

if [ "$#" -gt 1 ]
then
        read `print "VARNAME%04d" $N` <<EOF
$1
EOF
        shift
fi