Re: how to do a loop in idl using multi-commmand? [message #43516] |
Sun, 17 April 2005 22:37 |
Dick Jackson
Messages: 347 Registered: August 1998
|
Senior Member |
|
|
Hi,
"lixiaoyao" <lixiaoyao5880@yahoo.com> wrote in message
news:1113584939.738412.136930@o13g2000cwo.googlegroups.com.. .
> I can run this in my idl,who know how to solve it?
> nH2=findgen(10001)*10.0+0.1
> ratio=make_array(10001)
> for i=0,10000 do
> x=2*nH2[i]
> y=x*nH2[i]
> ratio[i]=x/y
> openw,1,'file'
> printf,1,ratio
> close,1
> it seems to show you can not do multicommand in do loop.
> thanks
The suggestions that others have given are all great, but you may want to
know that you can do the work of this loop using IDL's powerful (and much
faster) array operations as follows:
nH2=findgen(10001)*10.0+0.1
x=2*nH2
y=x*nH2
ratio=x/y
or, if you don't need to make x and y for any other reason:
ratio = (2*nH2) / (2*nH2*nH2)
or then, by factoring:
ratio = 1 / nH2
A useful section in IDL Online Help that covers some features that are
different from many other languages is "Writing Efficient IDL Programs" (in
Programming in IDL:Basics of IDL Programming).
Cheers,
--
-Dick
Dick Jackson / dick@d-jackson.com
D-Jackson Software Consulting / http://www.d-jackson.com
Calgary, Alberta, Canada / +1-403-242-7398 / Fax: 241-7392
|
|
|
|
|
|
Re: how to do a loop in idl using multi-commmand? [message #43523 is a reply to message #43521] |
Sat, 16 April 2005 11:41  |
mperrin+news
Messages: 81 Registered: May 2001
|
Member |
|
|
lixiaoyao <lixiaoyao5880@yahoo.com> wrote:
> it is still wrong. Any other suggestions?
> % Attempt to subscript NH2 with I is out of range.
> % Execution halted at: $MAIN$
> % Attempt to subscript NH2 with I is out of range.
> % Execution halted at: $MAIN$
> % Variable is undefined: Y.
> % Execution halted at: $MAIN$
>
> end
endfor, sorry.
That's what I get for thinking "This is so simple, I don't need
to test this code before I post it. " ;-)
|
|
|
|
|
Re: how to do a loop in idl using multi-commmand? [message #43529 is a reply to message #43525] |
Fri, 15 April 2005 12:55  |
Paul Selby
Messages: 5 Registered: August 2003
|
Junior Member |
|
|
Benjamin Hornberger wrote:
>
> If you want a FOR loop on the command line or in a batch file, you'll
> have to write it in one line, combining statements by "&":
>
> FOR I=0, 10000 DO BEGIN & x=2*nH2[i] & y=x*nH2[i] & ratio[i]=x/y & ENDFOR
>
> Note that the news client might have introduced a line break. This must
> be all in one line! I guess you realize it's better (more elegant, more
> understandable) to put it into a main-level or named program.
>
> Good luck,
> Benjamin
Actually you can put the statements on separate lines by using the line
continuation character "$" but it looks a bit messy
FOR I=0, 10000 DO BEGIN & $
x=2*nH2[i] & $
y=x*nH2[i] & $
ratio[i]=x/y & $
ENDFOR
Paul
|
|
|
Re: how to do a loop in idl using multi-commmand? [message #43530 is a reply to message #43529] |
Fri, 15 April 2005 12:49  |
Benjamin Hornberger
Messages: 258 Registered: March 2004
|
Senior Member |
|
|
If you do @filename, it runs as a batch file, meaning every statement,
line by line, is executed as if they were typed on the command line one
by one. You get the same error as on the command line.
What was meant in the previous post is a main-level program which has to
have an END statement and is run by ".run filename".
Go to the IDL help index and search for "batch file", then choose the
subentry "defined". You'll find a description of batch files, main-level
programs and named programs. David Fanning's book also describes it in
detail.
If you want a FOR loop on the command line or in a batch file, you'll
have to write it in one line, combining statements by "&":
FOR I=0, 10000 DO BEGIN & x=2*nH2[i] & y=x*nH2[i] & ratio[i]=x/y & ENDFOR
Note that the news client might have introduced a line break. This must
be all in one line! I guess you realize it's better (more elegant, more
understandable) to put it into a main-level or named program.
Good luck,
Benjamin
lixiaoyao wrote:
> I use @filename.pro to run it,it is still wrong. any other suggestion?
> for i=0,10000 do
> ^
> % Syntax error.
> At: /n/a/liletian/test.pro, Line 3
> % Variable is undefined: I.
> % Execution halted at: $MAIN$
> % Variable is undefined: I.
> % Execution halted at: $MAIN$
> % Variable is undefined: Y.
> % Execution halted at: $MAIN$
>
> endfor
> ^
> % Syntax error.
> At: /n/a/liletian/test.pro, Line 9
>
> end
> ^
> % Syntax error.
> At: /n/a/liletian/test.pro, Line 13
>
|
|
|
Re: how to do a loop in idl using multi-commmand? [message #43531 is a reply to message #43530] |
Fri, 15 April 2005 12:04  |
lixiaoyao
Messages: 49 Registered: April 2005
|
Member |
|
|
it is still wrong. Any other suggestions?
% Attempt to subscript NH2 with I is out of range.
% Execution halted at: $MAIN$
% Attempt to subscript NH2 with I is out of range.
% Execution halted at: $MAIN$
% Variable is undefined: Y.
% Execution halted at: $MAIN$
end
^
% Syntax error.
At: /n/a/liletian/test.pro, Line 7
|
|
|
Re: how to do a loop in idl using multi-commmand? [message #43535 is a reply to message #43531] |
Fri, 15 April 2005 11:12  |
lixiaoyao
Messages: 49 Registered: April 2005
|
Member |
|
|
I use @filename.pro to run it,it is still wrong. any other suggestion?
for i=0,10000 do
^
% Syntax error.
At: /n/a/liletian/test.pro, Line 3
% Variable is undefined: I.
% Execution halted at: $MAIN$
% Variable is undefined: I.
% Execution halted at: $MAIN$
% Variable is undefined: Y.
% Execution halted at: $MAIN$
endfor
^
% Syntax error.
At: /n/a/liletian/test.pro, Line 9
end
^
% Syntax error.
At: /n/a/liletian/test.pro, Line 13
|
|
|
Re: how to do a loop in idl using multi-commmand? [message #43536 is a reply to message #43535] |
Fri, 15 April 2005 10:19  |
savoie
Messages: 68 Registered: September 1996
|
Member |
|
|
I think you want some block statements here.
Save this to afile.pro and try ".run afile.pro"
***************
nH2=findgen(10001)*10.0+0.1
ratio=make_array(10001)
for i=0,10000 do begin
x=2*nH2[i]
y=x*nH2[i]
ratio[i]=x/y
endfor
openw,1,'file'
printf,1,ratio
close,1
end
***************
"lixiaoyao" <lixiaoyao5880@yahoo.com> writes:
> I can run this in my idl,who know how to solve it?
> <snip/>
> it seems to show you can not do multicommand in do loop.
hth
Matt
--
Matthew Savoie - Scientific Programmer
National Snow and Ice Data Center
(303) 735-0785 http://nsidc.org
|
|
|
Re: how to do a loop in idl using multi-commmand? [message #43537 is a reply to message #43536] |
Fri, 15 April 2005 10:15  |
mperrin+news
Messages: 81 Registered: May 2001
|
Member |
|
|
lixiaoyao <lixiaoyao5880@yahoo.com> wrote:
> I can run this in my idl,who know how to solve it?
> nH2=findgen(10001)*10.0+0.1
> ratio=make_array(10001)
> for i=0,10000 do
> x=2*nH2[i]
> y=x*nH2[i]
> ratio[i]=x/y
> openw,1,'file'
> printf,1,ratio
> close,1
> it seems to show you can not do multicommand in do loop.
You need to add "begin" and "end" around the commands you want to loop.
nH2=findgen(10001)*10.0+0.1
ratio=make_array(10001)
for i=0,10000 do begin
x=2*nH2[i]
y=x*nH2[i]
ratio[i]=x/y
end
openw,1,'file'
printf,1,ratio
close,1
|
|
|