arrays and needing length of fields

I have a sort of complex problem that I just can't figure out. I have data coming into a ksh program in a format that I need to parse out and display into a different format into a text file for printing. I have figured out how to get all the data in the format I need it in for the text file. The problem I am having is getting the columns to line up. They all need to be right justified and be as big as the max length of the data in each column. I won't know that until all the data is in the array. I don't know if I am explaining it well. I am going to try with a picture.

Col1 Col2 Col3 Col4 Col5
12345 12345 44 0 234
222222222 444 0 4444444 666666888999

This is how the data is coming out now. I am going to put it into an array, but have not done this yet. I don't know the length of each of the rows until I get the data.

So, each row is a separate column with its own length. I have just put spaces in between each column for now. I need it to look like this:

    Col1   Col2 Col3      Col4              Col5
 12345 12345  44           0               234

222222222 444 0 4444444 666666888999

As you can see I don't know what is the max length of each column until all the data is in. I need it to be right justified. I have no idea how to do this.

Is this a good explanation? I hope someone can help me. Thank you so much.

Allyson

use printf

I was printing out each line as it came in and I know I can't do that since I don't know the length. So, if I put it all into an array like this:

array[0]=first row with all columns separated by a space
array[1]=second row with all columns separated by a space
.
.
.

Then when I want to print it, how do I get each individual column out? Is there such a things as a two-dimensional array in ksh? Is so, I think that would be my best bet.

I am keeping the max length in a sort of hash with the column as the key and getting the length of each value and overwriting with the max one.

If there is a 2 dimensional array in ksh, that might solve my problems.

Can I have an example of the printf statement you were talking about?

Thanks.

Allyson

input file

$ cat prinfdata
12345 12345 44 0 234
222222222 444 0 4444444 666666888999
awk '{ printf("%-12s %-6s %-3s %-12s %-25s\n", $1, $2 , $3, $4, $5)}' prinfdata

output

12345        12345  44  0            234
222222222    444    0   4444444      666666888999

I can't seem to make it look like I want it to here. I am going to do each column separately on a new line to show what I need, but really they are all together with Col1, Col2, Col3, Col4, Col5 being on the same row and so on with each row. They are all right justified so they appear on the way right side of the page.

[RIGHT]Col1
12345
222222222

Col2
12345
444

Col3
44
0

Col4
0
4444444

Col5
234
666666888999[/RIGHT]
Does this make anymore sense?

Thanks.

Allyson

can u pls show ur script

It is a bit confusing, but here is the part that I am trying to figure out:

      while [[ $i -le $meascount ]];do
        firstpass="$\{meascolumn\#\#<meas-column>\}"
        meascol="$\{firstpass%%</meas-column>\}"
        if [[ $meascol = $i ]];then
          firstpass="$\{measvalue\#\#<meas-value>\}"
          measval="$\{firstpass%%</meas-value>\}"
          measvallen=$\{\#measval\}
         \(\( measvallen>maxlen \)\) && maxlen=$measvallen
         typeset mlen_$\{meascol\}=$maxlen

         if \(\($\{finaldaytime\}<$\{finalstarttime\}\)\) ; then
            earlydate=$earlydate$meascol$colon$measval$dash
            typeset val_$\{meascol\}=$measval
            i=$\(\($i\+1\)\)
            break
          fi
          measlen=$\{\#measval\}
          collen=$\(eval echo \\$mlen_$\{i\}\)
          \(\( spaces=collen-measlen \)\);
          typeset -L$spaces colfill=" "
  •        eachlinevals="$\{eachlinevals\}$space$\{measval\}$\{colfill\}"
          read meascolumn
          if [[ $meascolumn = "</tlm-meas>"* ]];then
            while [[ $i < $\(\($meascount-1\)\) ]];do
              i=$\(\($i\+1\)\)
    
  •           eachlinevals="$\{eachlinevals\}$space$dash"
             fi
           done
           break
         fi
         read measvalue
         read limitsflag
       else
    
  •          eachlinevals="$\{eachlinevals\}$space$dash"
          fi
        fi
        i=$\(\($i\+1\)\)
      done
    

This without an array yet. I was trying to do it line by line first, but that doesn't seem to work. I tried to give the most relevant pieces of code, but there is much, much more. Thanks.

Allyson