Reading from a text file to create an array of arrays

I have a text file with a different number of integers on each line with the first number on each line being the line number. Like this:

1 37 79 164 155 32 87 . . . 69 198 196
2 123 134 10 141 13 12 . . . 162 128 30
3 48 123 134 109 41 17 . . . 104 194 54

I want to create an array for each line:

arr[1] = [37, 79, . . . 196]
arr[2] = [123, 134, … 30]

I’ve worked a little bit with csv files before but am having trouble with this task.

Thanks

obj = readcsv("filename.csv")
output = map(row -> slicedim(obj, 1, row), 1:size(obj, 1))
2 Likes

Thanks!

I wasn’t clear that this wasn’t a csv file (I had worked a little bit with some in the past). But with your help, I got what I wanted. I did this:

f = readdlm("filename.txt")

nodes_to_node = map(row -> slicedim(f, 1, row), 1:size(f, 1))
 
# Shift to the left
for i in 1 : length(nodes_to_node)
    for j in 2 : length(nodes_to_node[i])
        nodes_to_node[i][j-1] = nodes_to_node[i][j]
    end
    #eliminate duplicate at end
    pop!(nodes_to_nodes[i])
end

# Clear out empty values
for i in 1 : length(nodes_to_node)
    filter!(e->e≠"",nodes_to_node[i])
end
line2vec(line) = parse.(Int, split(line, ' ' )) # use [2:end] to drop the first element
[line2vec(line) for line in eachline(filename; keep = false) if !isempty(line)]
1 Like