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

Re: FORMAT codes and tabs



J. Ashmall:

I think I know why IDL is behaving as it is, but really don't have an
answer to how you can read both types of lines using the same format
code.  I entered a few commands at the command prompt:
--
IDL> line1 = '10/11/99' + STRING(9b) + ' 19 : 42'
IDL> READS, line1, FORMAT='(I0,x,I0)', dd,mm
IDL> help, dd, mm
DD              INT       =       10
MM              INT       =       19
--
This format does give us back two integers, but they are the wrong ones.
We get back the 10 from the date and the 19 from the time.  When you use
the format code I0, IDL differentiates the elements to be read using
white space.  It applies the code I0 to the first element it finds- in
this case this is '10/11/99'.  If you tell IDL to FIX this string, you
get:
---
DL> HELP, FIX('10/11/99')
<Expression>    INT       =       10
---
After reading the '10/11/99', IDL moves on to the next element.  In this
case, it is the '19'.  If we try and read in all elements, we get the
following:
---
IIDL> READS, line1, FORMAT='(I0,x,I0,x,I0)', dd,mm,yy
% Unable to apply format code I to input: ": 42".
% Execution halted at:  $MAIN$
---
After reading the first two elements, it moves on to the third- ': 42'.
This one it cannot convert to the input format of integer:
---
IDL> HELP, FIX(': 42')
% Type conversion error: Unable to convert given STRING to Integer.
% Detected at:  $MAIN$
<Expression>    INT       =        0
---
Because you have other string characters in your input (the '/'), I don't
really know how you would go about finding a general input format code.
My suggestion would be to just read the whole thing in as a string,
STRCOMPRESS it and STR_SEP it by single spaces.  You will end up with an
array that has the date as the first element, the hour as the second
element, a ':' as the third and the minutes as the last.  I know this
will require more processing and isn't very clean, but I don't know of
any other solution.  I would be interested to know if someone does figure
this out.

Hope this helps.

S.Thiel
beorabor@bemail.com
------------------
Justin Ashmall wrote:

> Dear All,
>
> I'm having trouble reading in a text file with dates and times in the
> form:
> 11/10/99<tab><sp>16<sp>:<sp>47
> where <tab> and <sp> represent tabs and spaces.
> Sometimes the day/months are consist of only 1 char (e.g. 3/4/99). I
> can write a separate FORMAT statement for each case but when I try to
> use I0 to deal with both cases it doesn't work and it seems to be due
> to the TAB char.
> Am I just being foolish or what?
> The following short program might hepl explain. I'm using IDL 5.2
> under NT4.0.
>
> Cheers,
>
> Justin
>
> pro test_read
>
> ;Create a Tab character
> tab = STRING(9B)
> ;Create 2 lines of dummy input
> line1 = '10/11/99' + tab + ' 19 : 42'
> line2 = '1/3/99' + tab + ' 19 : 42'
>
> ;This format works for days/months with 2 digits
> READS, line1, FORMAT='(I2,x,I2,x,I2,2x,I2,3x,I2)', dd,mm,yy,hh,mn
> print, dd,mm,yy,hh,mn
>
> ;This format works for days/months with 1 digit
> READS, line2, FORMAT='(I1,x,I1,x,I2,2x,I2,3x,I2)', d,m,yy,hh,mn
> print, d,m,yy,hh,mn
>
> ;How to use the same format code for both line1 and line2?
> ;Presumably need to use I0 to automatically adjust to different
> ;widths.
>
> ;Just trying to read in the first 3 numbers (the date)
> READS, line1, FORMAT='(I0,x,I0)', dd,mm ;This works
> print, dd,mm
> READS, line1, FORMAT='(I0,x,I0,x,I0)', dd,mm,yy ;This DOESN'T work
> READS, line1, FORMAT='(I0,x,I0,x,I2)', dd,mm,yy ;This DOESN'T work
> ;Is there something strange going on with the tab character?
>
> end