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

Home » Public Forums » archive » Re: Format codes for comma separated data
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: Format codes for comma separated data [message #57750] Thu, 20 December 2007 13:32
TFE is currently offline  TFE
Messages: 11
Registered: November 2001
Junior Member
Brian Larsen wrote:
> Here is a simple example of the issue:
>
> I made a test file called test_dat.txt that has a bunch of lines of
> a, b, 0, 1, 2, 3
> a, b, 0, 1, 2, 3
> a, b, 0, 1, 2, 3
> a, b, 0, 1, 2, 3
> a, b, 0, 1, 2, 3
> a, b, 0, 1, 2, 3
> a, b, 0, 1, 2, 3
>
>
> IDL> file = 'test_dat.txt'
> IDL> openr, lun, file, /get_lun
> IDL> a = strarr(6)
> IDL> readf, lun, a
> IDL> print, a
> a, b, 0, 1, 2, 3 a, b, 0, 1, 2, 3 a, b, 0, 1, 2, 3 a, b, 0, 1, 2, 3 a,
> b, 0, 1, 2, 3 a, b, 0, 1, 2, 3
>
>
> What I would hope to get would be:
> a, b, 0, 1, 2, 3
>

Brian,
this looks correct-
print, a[0] ;for the first line

Tom
Re: Format codes for comma separated data [message #57753 is a reply to message #57750] Thu, 20 December 2007 13:08 Go to previous message
Brian Larsen is currently offline  Brian Larsen
Messages: 270
Registered: June 2006
Senior Member
OK, I got it but I'm not sure it makes sense.

for the simple example above the format code:
IDL> readf, lun, dat, format='(2(A,x), 4(I,x))'
% End of input record encountered on file unit: 101.
% Execution halted at: $MAIN$
does not work, while the code:
IDL> readf, lun, dat, format='(2(A2,x), 4(I,x))'
does work
IDL> help, dat, /str
** Structure <1eaa8c4>, 6 tags, length=32, data length=32, refs=1:
A STRING 'a,'
B STRING 'b,'
C INT 0
D INT 1
E INT 2
F INT 3

So if you don't specify the length of the first string it takes
everything?!?!




Brian

------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
Re: Format codes for comma separated data [message #57754 is a reply to message #57753] Thu, 20 December 2007 12:35 Go to previous message
Brian Larsen is currently offline  Brian Larsen
Messages: 270
Registered: June 2006
Senior Member
> I usually use an "x" to represent commas. ;-)
>
> Unless, of course, you want to read them as something.
> But I usually just ignore them.

OK, but none of my format attempts are working... for my simple
example above it seems that format='(2A0,4I0)' should work, but it
doesn't.

IDL> dat = {a:'', b:'', c:0, d:0, e:0, f:0}
IDL> openr, lun, file, /get_lun
IDL> readf, lun, dat, format='(2A0,4I0)'
% End of input record encountered on file unit: 101.
% Execution halted at: $MAIN$





Brian

------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
Re: Format codes for comma separated data [message #57755 is a reply to message #57754] Thu, 20 December 2007 12:30 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Brian Larsen writes:

> Exactly, how do you include commas in the format statement? They seem
> to just be separators, or I am really slow today... I hope its not
> really slow, or maybe that ok as xmas is close :)

I usually use an "x" to represent commas. ;-)

Unless, of course, you want to read them as something.
But I usually just ignore them.

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Format codes for comma separated data [message #57756 is a reply to message #57755] Thu, 20 December 2007 12:25 Go to previous message
Brian Larsen is currently offline  Brian Larsen
Messages: 270
Registered: June 2006
Senior Member
> Well, why not use a FORMAT statement, then?


Exactly, how do you include commas in the format statement? They seem
to just be separators, or I am really slow today... I hope its not
really slow, or maybe that ok as xmas is close :)






Brian

------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
Re: Format codes for comma separated data [message #57757 is a reply to message #57756] Thu, 20 December 2007 12:22 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Brian Larsen writes:

> No the fields are the same line to line, readf is not breaking up the
> line.
>
> When you do
> IDL> dat = strarr(134,3)
> IDL> readf, lun, in, dat
> you dont get one number per array element, you get one line per
> element.

Well, why not use a FORMAT statement, then?

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Format codes for comma separated data [message #57759 is a reply to message #57757] Thu, 20 December 2007 12:21 Go to previous message
Brian Larsen is currently offline  Brian Larsen
Messages: 270
Registered: June 2006
Senior Member
Here is a simple example of the issue:

I made a test file called test_dat.txt that has a bunch of lines of
a, b, 0, 1, 2, 3
a, b, 0, 1, 2, 3
a, b, 0, 1, 2, 3
a, b, 0, 1, 2, 3
a, b, 0, 1, 2, 3
a, b, 0, 1, 2, 3
a, b, 0, 1, 2, 3


IDL> file = 'test_dat.txt'
IDL> openr, lun, file, /get_lun
IDL> a = strarr(6)
IDL> readf, lun, a
IDL> print, a
a, b, 0, 1, 2, 3 a, b, 0, 1, 2, 3 a, b, 0, 1, 2, 3 a, b, 0, 1, 2, 3 a,
b, 0, 1, 2, 3 a, b, 0, 1, 2, 3


What I would hope to get would be:
a, b, 0, 1, 2, 3


Issue make sense?


Brian

------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
Re: Format codes for comma separated data [message #57761 is a reply to message #57759] Thu, 20 December 2007 12:17 Go to previous message
Brian Larsen is currently offline  Brian Larsen
Messages: 270
Registered: June 2006
Senior Member
On Dec 20, 3:13 pm, David Fanning <n...@dfanning.com> wrote:
> Brian Larsen writes:
>> What I normally do is create a struct array and read a file into that
>> with each column having a structure tag so I can stay organized. But
>> I can't figure out the format codes to do that this time...
>
> Why not? Because they vary from line to line?
>

No the fields are the same line to line, readf is not breaking up the
line.

When you do
IDL> dat = strarr(134,3)
IDL> readf, lun, in, dat
you dont get one number per array element, you get one line per
element.





Brian

------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
Re: Format codes for comma separated data [message #57764 is a reply to message #57761] Thu, 20 December 2007 12:13 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Brian Larsen writes:

> What I normally do is create a struct array and read a file into that
> with each column having a structure tag so I can stay organized. But
> I can't figure out the format codes to do that this time...

Why not? Because they vary from line to line?

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Format codes for comma separated data
Next Topic: IDL 7 on Ubuntu

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

Current Time: Sat Oct 11 16:13:57 PDT 2025

Total time taken to generate the page: 0.96582 seconds