columns in a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • red

    columns in a text file

    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_va lue, 5);
    echo col2_value;

    function tabber($col1, $tabs=1){
    $result=$col1;
    $len=strlen($co l1);
    if($len>28)$tab s+=1;
    if($len==22)$ta bs+=1;
    if($len<1)$tabs-=1;
    if($len==7)$tab s-=1;
    for($i=$len; $i>0; $i=$i-7)
    $tabs=$tabs-1;
    for($j=1; $j<$tabs; $j++)
    $result.= "\t";
    // $result.="_".$l en."_".$tabs."_ ";// adds debug info to column 2
    return $result;
    }

    red



  • Geoff Berrow

    #2
    Re: columns in a text file

    I noticed that Message-ID: <xqaLd.62922$Yh 2.24361663@twis ter.nyc.rr.com>
    from red contained the following:
    [color=blue]
    >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 ?[/color]

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

    <pre>
    <?php

    $array1=array(" 123","123456789 0","12345678901 234567890","123 455676789909099 23232","1234567 890123456789012 345678901234567 89");
    $array2=array(" abc","123456789 0","12345678901 234567890","123 455676789909099 23232","dfjgvqd fjvh");

    $maxwidth=50;

    for($i=0;$i<cou nt($array1);$i+ +){
    $tab="";
    $tabs =ceil(($maxwidt h-strlen($array1[$i]))/8);
    for($j=0;$j<$ta bs;$j++){
    $tab.="\t";
    }
    print $array1[$i].$tab.$array2[$i]."\n";
    }

    ?>
    </pre>

    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    Working...