Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Julia object in cmd

Tags:

cmd

julia

I'm currently coding in Julia, and at some point, I have to run a .exe program at the Command Prompt. Let's name that program "x.exe". I decided to add the following line to my code for Julia to execute the program inside of the code:

run(pipeline('x.exe input.txt 5 500',stdout="output.txt"))

The code works perfectly, but I have to insert manually the values "5" and "500", which are respectively, the number of rows in the input.txt file and the number of items of each row in the input.txt file. They also are the number of rows and columns of an Array stored in Julia.

Is there a way for the code to read those numbers directly? I tried

writedlm("size.txt", transpose([size(Array)[1],size(Array)[2]])," ")

and then

run(pipeline('x.exe input.txt type size.txt',stdout="output.txt"))

but it don't work....

like image 363
coolsv Avatar asked Oct 27 '22 20:10

coolsv


1 Answers

You can use @sprintf, e.g. as follows:

julia> using Printf

julia> x = [[1,2,3], [4,5,6]]
2-element Array{Array{Int64,1},1}:
 [1, 2, 3]
 [4, 5, 6]

julia> a = @sprintf("%d", size(x)[1][1])
"2"

julia> b = @sprintf("%d", size(x[1])[1][1])
"3"

julia> run(pipeline(`x.exe input.txt $a $b`,stdout="output.txt"))

As you can see, Julia uses the same method to interpolate variables into commands as the shell does (see the Julia manual section on Running External Programs).

like image 162
Simon Avatar answered Nov 28 '22 08:11

Simon