X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 05 Oct 2009 06:41 PM by  anon
IDL Starting
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
05 Oct 2009 06:41 PM
    Dear all I am a new, (yes very new) student, attempting to learn how to Programming in IDL (I tried these on IDL Version 6.3). When I coded a simple program to get information from Keyboard, the code is exactly below: PRO GetInfor    ; do nothing    ;myArray END FUNCTION GetInput, myArray, nElements myArray=MAKE_ARRAY(nElements,/INTEGER ,value=0) FOR i=0, nElements DO BEGIN  READ, myArray[i] ENDFOR T = myArray RETURN, T END When I called getInput from the other Pro called Display.pro PRO Display  T=getInput(myArray,5)  FOR i=0,5 DO BEGIN   PRINT, T[i]  ENDFOR END The result is:     So I saw the for loop in the first function did not work at all. Please tell me what happened? And could you help me to solve it?   IDL> display 0 0 0 0 0Attempt to subscript MYARRAY with I is out of range. Execution halted at: GETINPUT 9 C:\IDLExercises\getinfor.pro DISPLAY 3 C:\IDLExercises\display.pro $MAIN$   Kind regards.

    Deleted User



    New Member


    Posts:
    New Member


    --
    20 Oct 2009 05:43 PM
    Dear tb6pham, The problem here is that you are running the looping index one more element than it should be. Basically, the following code will run correctly: FUNCTION GetInput, myArray, nElements myArray=MAKE_ARRAY(nElements,/INTEGER ,value=0) FOR i=0, nElements-1 DO BEGIN  READ, myArray[i] ENDFOR T = myArray RETURN, T END PRO Display  T=getInput(myArray,5)  FOR i=0,4 DO BEGIN   PRINT, T[i]  ENDFOR   END   You can see here that the index in the loop in the procedure "Display" and in the function "GetInput" runs now one less element. In the calling procedure: FOR i=0,4 DO BEGIN ... and in the function: FOR i=0, nElements-1 DO BEGIN ... That was the problem. Let me know if you need anything else. Cheers, Fernando  
    You are not authorized to post a reply.