[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading in text data



Brian Reardon (reardonb@my-deja.com) writes:

> I am reading in text data (columns and rows of numbers) and I would
> like to know if there is a more elegant way of doing it.  Currently, the
> user must specify how many columns there are.  In my case the number of
> columns is manually inserted into the first line of the file like this:
> 
> 3
> 0 1 2
> 1 2 3
> 2 3 4
> 3 4 5
> 4 5 6
> 5 6 7
> 6 7 8
> 7 8 9
> 8 9 10
> 9 10 11
> 
> The attached procedure reads in the data.  Is there a way to read in the
> data such that the user does not have to a priori know how many columns
> there are and such that IDL does not have to reserve a large amount of
> memory for the number of rows?

There are any number of ways to count columns and rows,
as I'm afraid you are about to find out.

But if you data is well-behaved and your taste
runs to the totally unsophisticated, you might
try the Count_Columns and Count_Rows functions
on my anonymous ftp site. For someone who is
suppose to be a professional programmer these
are laughable. But they seem to work for me
more often than not, so I continue to use them. :-)

Count_Columns uses the first line in the file as an
example of what follows. Count_Rows just counts until
it reaches the end of the file. On most of the files
I deal with, this is still orders of magnitude faster
than reading the data in a loop.

   ftp://www.dfanning.com/pub/dfanning/outgoing/idl_course/count_columns.pro
   ftp://www.dfanning.com/pub/dfanning/outgoing/idl_course/count_rows.pro

Cheers,

David

-- 
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155