Printing combinations of lines from different files

I have a number of jl files created from a series of ‘for’ loops, specifying different properties. They basically look something like

a, 1, 50
a, 1, 60
a, 1, 70
a, 2, 50
a, 2, 60
a, 2, 70
b, 1, 50
b, 1, 60

and so on, except each file specifies different properties. Is there a way to

  1. systematically pick out one line from each jl file to form all possible combinations (say the first line of files X, Y, and Z, and then the first line of X and Y, but the second line of Z, etc.)
  2. and automatically create a new folder that stores (or rather, prints) the lines from each combination?
    The idea is basically that I have lists of all possible outcomes (the lines) for different scenarios (the jl files), and I want to automate the process of combining different outcomes from the scenarios into a folder which can later be read by another jl file to produce a result.

I apologize beforehand if this question is unclear. I’m very new to Julia and am pretty inexperienced at coding, so any help would be deeply appreciated. Thank you.

I don’t fully understand the question but I will try to help anyway. Make a look up dictionary something like

lookuptable[folder_name] = "some_julia_file.jl"

Although I think you should write your parameters to the dictionary instead of multiple files. I hope this gives you a hint to find a solution. You can also try to explain little more details what you are trying to do.

Re: 2 (partially). I would do something like this to create the folder:

if !isdir(outputdir)
  mkdir(outputdir)  # creates the folder in case it doesn't exist
end

function _writetodir()
  # here put the code that writes the file
end

cd(_writetodir, outputdir) # temporarily access to outputdir just to do the writing stuff.

where “outputdir” is a String that contains the full path to the directory where you want the output file to be written.

Here’s some sample code:

using DelimitedFiles

# create sample data files
writedlm("file1.csv", 1:3)
writedlm("file2.csv", 'a':'d')
writedlm("file3.csv", 10:10:20)

# read data from files
files = filter(f -> occursin(r"file\d+\.csv", f), readdir())
data = readlines.(files)

# find and print all combinations
recurse(combo) = println(combo)
recurse(combo, cur, next...) =
    foreach(x -> recurse([combo; x], next), cur)

recurse(String[], data...)

It first creates three files, with the following data:

julia> data
3-element Array{Array{String,1},1}:
 ["1", "2", "3"]
 ["a", "b", "c", "d"]
 ["10", "20"]

Then reads the data back into memory, and recursively creates all combinations. Any number of files are supported. The output of the above is:

["1", "a", "10"]
["1", "a", "20"]
["1", "b", "10"]
...
["3", "c", "20"]
["3", "d", "10"]
["3", "d", "20"]

You’ll want to change the recurse(combo) method to store the combinations to disk instead of printing them.

2 Likes