simple question about bytes [message #16986] |
Thu, 02 September 1999 00:00  |
Gerhard D. Rappold
Messages: 10 Registered: March 1998
|
Junior Member |
|
|
Hi,
I want to check if a line in an ascii file starts with a number or
character. My idea was to read the first character and convert it to
"byte" and check if its in or out the range of '48b' and '58b'. The code
example is :
; check for header
header=''
readf, lun, header
firstchar=strmid(header,0,1)
first_byte=byte(firstchar)
no_head=0
head_index=0
WHILE ((first_byte LT 48b) AND (first_byte GT 57b)) DO BEGIN
readf, lun, header ; reads next line
head_index=head_index+1 ; counts header lines
no_header =head_index
ENDWHILE
...
it compiles fine but on runtime I get the errormessage :
Expression must be a scalar in this context: <BYTE Array(1)>
How can I change the variable 'first_byte' from byte array to a byte
scalar ?
Or any other hint for a solution would be great!
Thank's
Gerhard
|
|
|
Re: simple question about bytes [message #17076 is a reply to message #16986] |
Fri, 03 September 1999 00:00   |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
rappold wrote:
> Hi,
>
> I want to check if a line in an ascii file starts with a number or
> character. My idea was to read the first character and convert it to
> "byte" and check if its in or out the range of '48b' and '58b'. The code
> example is :
>
> ; check for header
> header=''
> readf, lun, header
> firstchar=strmid(header,0,1)
> first_byte=byte(firstchar)
>
> no_head=0
> head_index=0
> WHILE ((first_byte LT 48b) AND (first_byte GT 57b)) DO BEGIN
> readf, lun, header ; reads next line
> head_index=head_index+1 ; counts header lines
> no_header =head_index
> ENDWHILE
> ...
You have to use the first index type e.g.
WHILE ((first_byte[0] LT 48b) AND (first_byte[0] GT 57b)) DO BEGIN
you can declare header as bytarr with one element
header=bytarr(1)
readf, lun, header
first_byte=header[0]
R.Bauer
|
|
|
Re: simple question [message #20146 is a reply to message #16986] |
Fri, 19 May 2000 00:00  |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
Pavel Romashkin wrote:
>
> Isn't breaking out of DO loops the same as using conditional loops? FOR
> / DO have a particular purpose, to execute a certain number of times.
> For repeating under a condition, WHILE and REPEAT are provided, I think.
I agree. But both WHILE and REPEAT exit the conditional loop once a
specified condition occurs. Sometimes, you don't want to exit the loop,
just skip the current cycle and continue. The existence of WHILE and
REPEAT certainly limit the need for a BREAK type of statement, but not a
CONTINUE (or CYCLE). In addition, the WHILE and REPEAT don't provide you
with the current loop index (i.e. you have to do your own i = i + 1
within the loop). I guess one's preference for using WHILE/REPEATs or
FOR loops with CONTINUE/BREAKs stems non-trivially from personal
aesthetic.
paulv
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.202, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: simple question [message #20156 is a reply to message #16986] |
Thu, 18 May 2000 00:00  |
promashkin
Messages: 169 Registered: December 1999
|
Senior Member |
|
|
Isn't breaking out of DO loops the same as using conditional loops? FOR
/ DO have a particular purpose, to execute a certain number of times.
For repeating under a condition, WHILE and REPEAT are provided, I think.
Cheers,
Pavel
Paul van Delst wrote:
>
> Craig Markwardt wrote:
>>
>> "richard hilton" <rdh5@dmu.ac.uk> writes:
>>
>>> This probably sounds like a stupid question but does anybody know of an IDL
>>> equivilent to the CONTINUE command in C/C++ ? Your help would be much
>>> appreciated.
>>
>> I've wished for an equivalent to continue and break, but have never
>> found it. You will have to do use GOTO explicitly.
>>
>> for i = 0, n-1 do begin
>> if val(i) EQ 0 then goto, NEXT_VAL ;; equivalent to continue
>> compute_val, val(i)
>> if val(i) LT 0 then goto, DONE_VAL ;; equivalent to break
>> NEXT_VAL:
>> endfor
>> DONE_VAL:O
|
|
|
Re: simple question [message #20160 is a reply to message #16986] |
Thu, 18 May 2000 00:00  |
Paul van Delst
Messages: 364 Registered: March 1997
|
Senior Member |
|
|
Craig Markwardt wrote:
>
> "richard hilton" <rdh5@dmu.ac.uk> writes:
>
>> This probably sounds like a stupid question but does anybody know of an IDL
>> equivilent to the CONTINUE command in C/C++ ? Your help would be much
>> appreciated.
>
> I've wished for an equivalent to continue and break, but have never
> found it. You will have to do use GOTO explicitly.
>
> for i = 0, n-1 do begin
> if val(i) EQ 0 then goto, NEXT_VAL ;; equivalent to continue
> compute_val, val(i)
> if val(i) LT 0 then goto, DONE_VAL ;; equivalent to break
> NEXT_VAL:
> endfor
> DONE_VAL:
If there's one thing I loathe, it's GOTO'ing _out_ of DO/IF
loops/constructs. I, too, wish IDL would introduce statements such as
the CYCLE (equiv to CONTINUE) and EXIT (equiv to BREAK) statements in
F90/F95. Sure makes logic in DO loops and IF constructs supa-easy to
follow (although I have to admit the example above is clear and easy to
follow).
How would one begin the process of doing this, i.e. getting RSI to
introduce new control transfer statements such as CYCLE and BREAK? I
can't see that it would be a terribly difficult thing to do but I don't
write compiler type stuff.
paulv
--
Paul van Delst Ph: (301) 763-8000 x7274
CIMSS @ NOAA/NCEP Fax: (301) 763-8545
Rm.202, 5200 Auth Rd. Email: pvandelst@ncep.noaa.gov
Camp Springs MD 20746
|
|
|
Re: simple question [message #20162 is a reply to message #16986] |
Thu, 18 May 2000 00:00  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
"richard hilton" <rdh5@dmu.ac.uk> writes:
> This probably sounds like a stupid question but does anybody know of an IDL
> equivilent to the CONTINUE command in C/C++ ? Your help would be much
> appreciated.
I've wished for an equivalent to continue and break, but have never
found it. You will have to do use GOTO explicitly.
for i = 0, n-1 do begin
if val(i) EQ 0 then goto, NEXT_VAL ;; equivalent to continue
compute_val, val(i)
if val(i) LT 0 then goto, DONE_VAL ;; equivalent to break
NEXT_VAL:
endfor
DONE_VAL:
...
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|