473,394 Members | 1,845 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,394 software developers and data experts.

Counting columns in file

I have a data file which has not fixed coulmns as follows:

Column11 Column12 Column13
Column21 Column22
Column31 Column32 Column33 Column34
.
.
.

I want to know what is the maximum number of columns in each raw and I want to fill all the other missing columns with "NONE" to make my file with fixed column numbers so I can use it in MATLAB.

Thanks alot
Sep 23 '07 #1
5 7562
KevinADC
4,059 Expert 2GB
What have you tried so far?
Sep 23 '07 #2
I'm still new in perl. I only can count raws by spliting each line but so far I don't know how to deal with columns especially that I don't know how many are they in each raw. This code only give me the total raws in the file not the column.

Expand|Select|Wrap|Line Numbers
  1. my $COLUMN;
  2. my $MaxColumn;
  3.  
  4. open (IN, 'c:\database.txt'); 
  5. while (<IN>) { 
  6.    chomp($line = $_);
  7.     @COLUMN = split(/\t/,$line); # split tab 
  8. $MaxColumn++;
  9. }
  10. printf "Total = $MaxColumn \n";
  11.  
I wish that anyone can help me.
thanks
Sep 24 '07 #3
KevinADC
4,059 Expert 2GB
The length of the array is the number of columns:

@COLUMN = split(/\t/,$line); # split tab
print "There are ", scalar @COLUMN, " columns in this line";

Did you need each row in the file to have the same number of columns?
Sep 24 '07 #4
thank you very much. I added this line:
$ColumnSize=@COLUMN;
and it works very fine. Thanks really.

Now I need also to make every raw with the same number of columns by adding (-1) or (NONE) for the new columns without affecting the original data in each existed column.

I'm really thankful
Sep 24 '07 #5
I think I solved it by adding these lines:

Expand|Select|Wrap|Line Numbers
  1. open(TARGET,"> new.txt"); 
  2. while ($size < $MaxColumn) { 
  3.        $COLUMN[$size] = -1;
  4. $line = join("\t",@COLUMN);
  5. $size++;
  6.  
  7.   }
  8. print TARGET $line ."\n";
  9.  
Thank you alot for helping.
Sep 24 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: John Reese | last post by:
def uselessHash(filename): fp= open(filename) hash= 0 for line in fp: hash ^= hash(line.strip()) fp.close() # do I need this or is fp closed by ref count? return hash Consider the function...
4
by: Regnab | last post by:
As any good code pirate would, I have duly copied the code suggested in this forum from 1998(VBA method to determine number of columns in a table??) but I keep getting an error "Object Required"......
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
1
by: JJ | last post by:
Is it possible to count the number of columns that match certain conditions and return a single value? For example in the database we have a record where: Column 1 = Male Column 2 = A Column...
7
by: archana | last post by:
Hi all, I have csv file which i want to show into datatable. when i try to use oledb to copy content of csv file into datatable not all rows are getting copied into datatable. Some last rows...
2
by: justin | last post by:
Hi , I have the following code which reads the first line of the Excel file and comes out of the loop. The test.xls file conmatains two columns in which first column has numerical data and...
2
by: =?ISO-8859-2?Q?Gregor_Kova=E8?= | last post by:
Hi! I have a file TABLE1.IXF and want to know how many rows are in there. How to do it? Thanks and best regards, Kovi -- ____________________________
7
by: crochunter | last post by:
Hi I was trying to count rows and columns in a tab delimited flat file. Like here in example below i should be able to detect automatically the no of rows and columns. So I should get 5 columns...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.