The ARGS keyword allows arguments to be specified when calling a Julia script from the command line (see Access command line arguments in Julia). With this, each space in the command line call marks a separate element in the ARGS Array. What if I want one of these elements to itself be an Array of arbitrary length? Just concatenating separate elements of ARGS seems unsuitable here.
-
1The answer provided is good, but just thought I'd mention that another option is to store the array in a text file, e.g. csv, in a known location and just pass the location in as an argument. Then simply make `readcsv` one of the first steps of your script. – Colin T Bowers May 28 '16 at 00:02
-
1Indeed! Good suggestion. In my case, I've got a small array that defines how the script will run, and I'm actually then writing python code to generate a whole bunch of command line calls over different configuration patterns to run my Julia script. In other situations though your suggestion could well be the most useful, particularly for larger arrays. Thanks! – Michael Ohlrogge May 28 '16 at 02:14
-
@ColinTBowers If you add your comment as an answer I'd be happy to upvote it. I think other uses might find it helpful and I know that I frequently don't see comments as visibly as answers when I search on SO. – Michael Ohlrogge May 28 '16 at 14:46
-
Done, and I added a bit of window-dressing. Cheers. – Colin T Bowers May 28 '16 at 23:59
2 Answers
There are two keys here.
- Note that anything you pass to the script from the command line will initially be a string. Use
eval(parse( ))
within the script to turn the string that you pass into the type of object you desire. - Make sure to formulate that initial string correctly. This may depend upon the contents of the Array you wish to pass.
Here is an example script:
println(string("First Arg Passed to Script: ", ARGS[1]))
println(string("typeof(ARGS[1]) = ", typeof(ARGS[2])))
println(string("Second Arg Passed to Script: ", ARGS[2]))
println(string("typeof(ARGS[2]) = ", typeof(ARGS[2])))
MyArray = eval(parse(ARGS[1]))
println(string("typeof(MyArray) = ", typeof(MyArray)))
println(string("length(MyArray) = ", length(MyArray)))
println(string("MyArray = ", MyArray))
Running this with the following terminal command:
julia /Users/aireties/Desktop/ArrayTest.jl "[2, 4, 5]" 23
yields the output:
First Arg Passed to Script: [2, 4, 5]
typeof(ARGS[1]) = UTF8String
Second Arg Passed to Script: 23
typeof(ARGS[2]) = UTF8String
typeof(MyArray) = Array{Int64,1}
length(MyArray) = 3
MyArray = [2,4,5]
Note: however, if I wish to include strings within the Array that I pass from the command line, I need to use a second, single set of quotations, encasing the Array with the double quotations that Julia uses to indicate strings:
julia /Users/aireties/Desktop/ArrayTest.jl '["a", "b", 5]' 23
A call of:
julia /Users/aireties/Desktop/ArrayTest.jl "["a", "b", 5]" 23
Would yield an error.

- 10,559
- 5
- 48
- 76
The answer provided by @aireties is good. However, I thought I'd provide another option that works especially well if the input array is large. Simply store your input array in a text file in a known location, e.g. /someAbsolutePath/myArray.csv
and then pass in the location of the text-file as an argument when you call julia
. Then, in the first few lines of your script, call readcsv
and voila.
This is actually a fairly common tactic for many languages like Julia (e.g. Python/R/Matlab) when you want to call a routine from the command line that takes a lot of inputs. I've seen compiled Matlab routines in production environments that take the location of a custom text file as the single input argument at the command line. The first section of the Matlab routine would then read in a whole host of input arguments from the custom text file (and throw an error if the text file did not look as expected). In some cases, the text file would contain the locations of lots of other text files that contained other relevant inputs!

- 18,106
- 8
- 61
- 89