comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: reading in | delimited files into multiple arrays
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: reading in | delimited files into multiple arrays [message #49003] Sat, 10 June 2006 10:45
Marshall Perrin is currently offline  Marshall Perrin
Messages: 44
Registered: December 2005
Member
johnadams_1980@yahoo.com <johnadams_1980@yahoo.com> wrote:
> Hi All,
>
> How do you read in a | delimited file in to multiple arrays, one for
> each column of the data?

'readcol' from the Goddard IDL astronomy library does just this:

readcol,'filename.txt', delim='|', a1, a2 , a3 ,a4

- Marshall
Re: reading in | delimited files into multiple arrays [message #49019 is a reply to message #49003] Fri, 09 June 2006 09:30 Go to previous message
Jo Klein is currently offline  Jo Klein
Messages: 54
Registered: January 2006
Member
> How do you read in a | delimited file in to multiple arrays, one for
> each column of the data?

I guess there are many ways to do this - how about splitting each line
using strsplit? Is the number of columns per line fixed? You could for
example first find out the number of lines to process using FILE_LINES,
then provide an array for intermediate storage:

number_of_columns=5
myfile='something.txt'
n=file_lines(myfile)
arr=make_array(5,n,/STRING)
openr,unit,myfile,/get_lun

for i=0,n-1 do BEGIN
readf,unit,line
arr[*,i]=strsplit(line,'|',/extract)
endfor
close,unit
free_lun,unit

Then, you can reform this into individual arrays, eg.:

secondcolumn=reform(arr[1,*])

Haven't tested this, but give it a try.
Cheers,
Jo
Re: reading in | delimited files into multiple arrays [message #49020 is a reply to message #49019] Fri, 09 June 2006 09:29 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
johnadams_1980@yahoo.com writes:

> How do you read in a | delimited file in to multiple arrays, one for
> each column of the data?

data = FltArr(numCol, numRow)
OpenR, lun, theFile, /Get_Lun
ReadF, lun, data
Free_Lun, data

col_1 = Reform(data[0,*])
col_2 = Reform(data[1,*])

etc.

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Roses Blooming
Next Topic: replace elements in array

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sat Oct 11 15:23:19 PDT 2025

Total time taken to generate the page: 0.24188 seconds