473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

columns in a text file

red
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){
$result=$col1;
$len=strlen($col1);
if($len>28)$tabs+=1;
if($len==22)$tabs+=1;
if($len<1)$tabs-=1;
if($len==7)$tabs-=1;
for($i=$len; $i>0; $i=$i-7)
$tabs=$tabs-1;
for($j=1; $j<$tabs; $j++)
$result.= "\t";
// $result.="_".$len."_".$tabs."_";// adds debug info to column 2
return $result;
}

red

Jul 17 '05 #1
1 1904
I noticed that Message-ID: <xq**********************@twister.nyc.rr.com>
from red contained the following:
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 ?


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

<pre>
<?php

$array1=array("123","1234567890","1234567890123456 7890","12345567678990909923232","12345678901234567 8901234567890123456789");
$array2=array("abc","1234567890","1234567890123456 7890","12345567678990909923232","dfjgvqdfjvh");

$maxwidth=50;

for($i=0;$i<count($array1);$i++){
$tab="";
$tabs =ceil(($maxwidth-strlen($array1[$i]))/8);
for($j=0;$j<$tabs;$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/
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Romulo NF | last post by:
I´ve seen some people around the web wondering about a code to reorder table columns using the mouse (drag and drop). Since i had already made one script to reorder table lines (TR´s) i decided to...
5
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
6
by: napatel04 | last post by:
Hi everyone, I would like to know if there is a quick query someone can help me write for the following scenario. I think I can do this with VBA but since this is suppose to be a temp. solution,...
2
by: jthep | last post by:
How can I use bulk insert to insert a text file where the columns in the text file is in different order than the columns in the table? I have a ZIP table with Zip_Code, Zip_City, Zip_State and...
3
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.