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

Re: Reading in text data



reardonb@my-deja.com wrote:
> 
> Hi.  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

There are lots of ways to attack this problem depending upon 
whether you need to operate on the data one-row-at-a-time, or
if you need all the data stored into an array.  Some potentially
useful ideas are illustrated in this simple procedure...

infile = 'column.dat'                 ;** ASCII input filename.
spawn, 'wc -l < ' + infile, num_recs  ;** Number of rows in ASCII file
num_recs = long(num_recs(0))          ;** Convert to num_recs to LONG
data = strarr(num_recs)               ;** Declare output array
openr, iu, infile, /get_lun           ;** Open file
readf, iu, data                       ;** Real ALL the data as a string
free_lun, iu                          ;** Close the file

;**  Use help, to check size of arrays.  Notice record #1.
for i = 0, num_recs-1 do help, long(str_sep(data(i),' ',/rem))

;**  Or use print, to check on value of array elements.
for i = 0, num_recs-1 do print, long(str_sep(data(i),' ',/rem))

end

-- 
Andrew Loughe =====================================================
NOAA/OAR/FSL/AD   R/FS5  |  email: loughe@fsl.noaa.gov
325 Broadway             |  wwweb: www-ad.fsl.noaa.gov/users/loughe
Boulder, CO  80305-3328  |  phone: 303-497-6211   fax: 303-497-6301