columns in a text file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have two columns of strings of various lengths.
I want the second column to be straight, so I need to find the correct
number of tabs between the two columns for each row.I don't mind
starting with a wide column with a preset number of tabs and adjusting
it statically to fit my data, so I don't need to get the length of the
longest string in the first column before I start.

Through trial and error, I have an algorithm that seems to work. There's
a pattern in there but I don't see the logic. Does anyone have a
more graceful algorithm for this, the way it should be done ?
for each row, I use this:

echo tabber($col1_value, 5);
echo col2_value;

function tabber($col1, $tabs=1){
   for($i=$len; $i>0; $i=$i-7)
   for($j=1; $j<$tabs; $j++)
     $result.= "\t";
     // $result.="_".$len."_".$tabs."_";// adds debug info to column 2
     return $result;


Re: columns in a text file

from red contained the following:

Quoted text here. Click to load it

Dunno about that, but here is a version using two arrays to simulate the
column data.




$tabs =ceil(($maxwidth-strlen($array1[$i]))/8);
print $array1[$i].$tab.$array2[$i]."\n";


Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs /

Site Timeline