I've got what seems like a simple FOREACH loop in IDL (version 8.2.2). For the life of me I can't see why I'm getting a syntax error. I tried replacing the FOREACH with a simple FOR loop with the same results.
Removing the for loop and running the statements alone works fine (hard coding a single value for file of course).
FOREACH file, filenames DO BEGIN
; A number of
; statements that execute
; just fine
ENDFOREACH
Result:
ENDFOREACH
^
% Syntax error.
This is all running in a script, called with #myscript
Perhaps an even simpler example straight from the documentation will help:
I created a script test.pro, copy/paste from the docs: http://www.harrisgeospatial.com/docs/BEGIN___END.html, the contents of test.pro are:
arr = [1, 3, 5, 7, 9]
FOREACH element, arr DO BEGIN
PRINT, element
ENDFOREACH
Result:
IDL> #test
9
ENDFOREACH
^
% Syntax error.
At: /mydir/test.pro, Line 4
IDL>
Batch files (called via #myscript) can't have compound statements, i.e. with BEGIN/END. Make it into a procedure/function or a main-level program.
Related
By mistake, I typed something like this:
for f in a b; echo a; echo x
The output produced was
a
a
x
as if I had written
for f in a b; do echo a; done; echo x
Could someone explain, why my code produced this example? Checking the man page, it clearly says that the required syntax has to be
for name ... [ in word ... ] term do list done
No shortcut explains that I could leave out the do or done.
I'm running zsh 5.8
Somewhat confusingly, that syntax is not described in the section that introduces the for loop. Instead it's listed with some other short command versions, in a separate part of the zshmisc man page titled ALTERNATE FORMS FOR COMPLEX COMMANDS.
for name ... [ in word ... ] term sublist
where term is at least one newline or ;. Another short form of for.
The introduction to the section mentions some caveats:
Many of zsh's complex commands have alternate forms. These
are non-standard and are likely not to be obvious even to seasoned
shell programmers; they should not be used anywhere that portability
of shell code is a concern.
The short versions below only work if sublist is of the form `{ list
}' or if the SHORT_LOOPS option is set.
The following line of code is a recursive helper function for generating the nth Fibonacci number. From Julia's REPL include("filename.jl"). It's the only line of code in the file and it won't compile. Julia ver. 4.5 and 5.1. Have tried using bracketing parentheses and also have rewritten in standard if-else format, all to no avail. This code will simply not compile for me. Can anyone see what I'm doing wrong?
helper(current, next, n) = n==0 ? current : helper(next, current+next, n-1)
CREATE OR REPLACE PROCEDURE ex9a(n NUMBER ,c CHAR) IS
pi NUMBER(7,4):=3.14;
v_record Areas%rowtype;
BEGIN
IF c='R' THEN
DBMS_OUTPUT.PUT_LINE('CHOICE : R');
v_record.Input_Value:= n;
v_record.Circle_Area:=pi*n*n;
v_record.Square_Area:=null;
v_record.Sphere_Area:=2*pi*n;
v_record.Sphere_Volume:=(3/4)*r*r;
v_record.Cube_Volume:=null;
END IF;``
END;
/
I am getting procedure created with compilation errors.I want to compute area of a circle and insert it into the table .
When I give SHOW ERRORS , it lists the errors as
1) plsql statement
2) 'R' must be declared .
(Even after I gave 'then'. I forgot the line number)
It would help if you would list the errors, and the lines on which they occur.
However, one obvious issue is that your IF statement has no THEN. It should be:
IF c = 'R' THEN
What can I do within a file "example.jl" to exit/return from a call to include() in the command line
julia> include("example.jl")
without existing julia itself. quit() will just terminate julia itself.
Edit: For me this would be useful while interactively developing code, for example to include a test file and return from the execution to the julia prompt when a certain condition is met or do only compile the tests I am currently working on without reorganizing the code to much.
I'm not quite sure what you're looking to do, but it sounds like you might be better off writing your code as a function, and use a return to exit. You could even call the function in the include.
Kristoffer will not love it, but
stop(text="Stop.") = throw(StopException(text))
struct StopException{T}
S::T
end
function Base.showerror(io::IO, ex::StopException, bt; backtrace=true)
Base.with_output_color(get(io, :color, false) ? :green : :nothing, io) do io
showerror(io, ex.S)
end
end
will give a nice, less alarming message than just throwing an error.
julia> stop("Stopped. Reason: Converged.")
ERROR: "Stopped. Reason: Converged."
Source: https://discourse.julialang.org/t/a-julia-equivalent-to-rs-stop/36568/12
You have a latent need for a debugging workflow in Julia. If you use Revise.jl and Rebugger.jl you can do exactly what you are asking for.
You can put in a breakpoint and step into code that is in an included file.
If you include a file from the julia prompt that you want tracked by Revise.jl, you need to use includet(.
The keyboard shortcuts in Rebugger let you iterate and inspect variables and modify code and rerun it from within an included file with real values.
Revise lets you reload functions and modules without needing to restart a julia session to pick up the changes.
https://timholy.github.io/Rebugger.jl/stable/
https://timholy.github.io/Revise.jl/stable/
The combination is very powerful and is described deeply by Tim Holy.
https://www.youtube.com/watch?v=SU0SmQnnGys
https://youtu.be/KuM0AGaN09s?t=515
Note that there are some limitations with Revise, such as it doesn't reset global variables, so if you are using some global count or something, it won't reset it for the next run through or when you go back into it. Also it isn't great with runtests.jl and the Test package. So as you develop with Revise, when you are done, you move it into your runtests.jl.
Also the Juno IDE (Atom + uber-juno package) has good support for code inspection and running line by line and the debugging has gotten some good support lately. I've used Rebugger from the julia prompt more than from the Juno IDE.
Hope that helps.
#DanielArndt is right.
It's just create a dummy function in your include file and put all the code inside (except other functions and variable declaration part that will be place before). So you can use return where you wish. The variables that only are used in the local context can stay inside dummy function. Then it's just call the new function in the end.
Suppose that the previous code is:
function func1(...)
....
end
function func2(...)
....
end
var1 = valor1
var2 = valor2
localVar = valor3
1st code part
# I want exit here!
2nd code part
Your code will look like this:
var1 = valor1
var2 = valor2
function func1(...)
....
end
function func2(...)
....
end
function dummy()
localVar = valor3
1st code part
return # it's the last running line!
2nd code part
end
dummy()
Other possibility is placing the top variables inside a function with a global prefix.
function dummy()
global var1 = valor1
global var2 = valor2
...
end
That global variables can be used inside auxiliary function (static scope) and outside in the REPL
Another variant only declares the variables and its posterior use is free
function dummy()
global var1, var2
...
end
I'm trying to write a batch file that performs operations depending on the result of a modulus operation performed on a set variable. However, I can't seem to get it quite right.
To first of all test my syntax for the mathematical operation, I've been trying to get a simpler script to produce desired results.
:START
SETLOCAL
SET /P Input-Num="Input Number: "
SET /A Input-Num=%Input-Num% %% 2
ECHO %Input-Num%
ENDLOCAL
PAUSE
:END
If I input 5, the expected output is 1. However, instead I get a message saying Missing operator. and then it outputs 5.
What am I doing wrong here?
Using SET /P is your problem, as 5 is no longer treated as a numerical value. Your example as above works as expected