X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 09 Jun 2011 04:02 PM by  anon
running selected text in idlde
 2 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
09 Jun 2011 04:02 PM
    i have tried to find an answer to this question on this board, because i don't think it's unique, but i haven't been able to find a thread along these lines. i am used to using idl in a linux environment, with one command line terminal and a text editor. in this way i could copy text from the editor and run it at the command line. i was hopeful that idlde, with it's shift+F8 shortcut, would make running code more simple. however, i can't get it to function as i would expect. when i select and run the following text: for x=0,3 do begin print,x print,'hello' endforit prints "4", "hello" and then the endfor gives a syntax error. in other words, the commands within the for loop never seem to run, at least within the loop. i've also preceded this code with a ".run" and followed it with "end" and it acted as if nothing had been typed within the .run command. when i paste that same code (with the .run call) into a linux/dos idl command prompt it acts "properly", printing 0, 1, 2 and 3, separated by "hello" lines. i think the issue is something in the way the idlde command prompt recognizes the end of a line because i can get it to execute properly if i end each line with "&$", or if i execute (or copy/paste) each line individually, rather than as a group of lines. obviously those shortcuts are simple for this 4-line example, but become more excruciating for larger code chunks. so how can i get loops to execute as i would anticipate using idlde? am i missing something fundamental. thanks in advance for any guidance. gh

    Deleted User



    New Member


    Posts:
    New Member


    --
    22 Jun 2011 11:56 AM
    The reason that this fails, is that it is a multi line FOR loop statement. In an IDL program, the "BEGIN" and "ENDFOR" statements tell the interpreter to execute each statement in-between, which can include multiple lines of code. However, when you "run selected text", really it is just copying the lines of code into the IDL command line, one line at a time and attempting to execute them. A multi-line FOR loop cannot be executed in this way. You would get the same error if you copied each line individually to the command line. However, you can work around this by converting this to a "single" line of code, by using "&" and "$" characters in the following way: for x=0,3 do begin $ print,x & $ print,'hello' & $ endfor Then when you "run selected text", IDL is interpreting this as a single line of code. Each "$" character indicates a line continuation, and each "&" indicates that the next command is also to be executed (execute "this" & "that"). The code above is equivalent to the following statement at the command line: IDL> for x=0,3 do begin print,x & print,'hello' & endfor That should allow you to execute your code in this way. -Josh ITTVIS

    Deleted User



    New Member


    Posts:
    New Member


    --
    17 Aug 2011 03:05 PM
    josh, thank you for the response, and apologies for my delayed response. i thought i understood how the command line worked before all this. i should say that this is all coding preference, but as you can imagine putting many loops into single lines of code so they can be executed can make for some ugly text. when using the command prompt in unix/dos i can type ".run", then copy the multiple lines of code (minus any & or $ characters), then type "end" at the end, and all lines of code between the ".run" and "end" will be executed. i have tried this three ways in idlde with different results. method 1: type ".run" at command line, highlight code to be executed, shift+f8, type "end" at command line. this prints "4" and "hello" method 2: type ".run" at command line, highlight code to be executed, copy/paste code to command prompt, type "end" at command line. same outcome as method 1. method 3: type ".run" at command line, highlight first line of code, copy/paste (or shift+f8) first line to command prompt, highlight second line of code, copy/paste (or shift+f8) second line of code to command prompt, repeat for lines 3 and 4, type "end" at command line. this time the result is "0", "hello", "1", "hello", "2", "hello", "3", "hello", each on it's own line. so it seems to me that for some reason the copy/paste mechanism for pasting/executing multiple lines is different than for single lines. additionally, when used with idlde for 7.X method 2 gave the same results as method 3. this is weird. gh
    You are not authorized to post a reply.