# Reading from a text file to create an array of arrays

**URL:** https://discourse.julialang.org/t/reading-from-a-text-file-to-create-an-array-of-arrays/11413
**Category:** New to Julia
**Created:** [June 4, 2018, 2:23pm UTC](https://discourse.julialang.org/t/reading-from-a-text-file-to-create-an-array-of-arrays/11413 "2018-06-04T14:23:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Tim\_G](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim_g/32/11039_2.png) [@Tim\_G](https://discourse.julialang.org/u/Tim_G)
#### Post date: [June 4, 2018, 2:23pm UTC](https://discourse.julialang.org/t/reading-from-a-text-file-to-create-an-array-of-arrays/11413/1 "2018-06-04T14:23:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [June 4, 2018, 2:32pm UTC](https://discourse.julialang.org/t/reading-from-a-text-file-to-create-an-array-of-arrays/11413/2 "2018-06-04T14:32:02Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![Tim\_G](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim_g/32/11039_2.png) [@Tim\_G](https://discourse.julialang.org/u/Tim_G)
#### Post date: [June 4, 2018, 6:27pm UTC](https://discourse.julialang.org/t/reading-from-a-text-file-to-create-an-array-of-arrays/11413/3 "2018-06-04T18:27:15Z")

</div>

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:

```julia
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
```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [June 5, 2018, 7:30am UTC](https://discourse.julialang.org/t/reading-from-a-text-file-to-create-an-array-of-arrays/11413/4 "2018-06-05T07:30:19Z")

</div>

```julia
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)]

```
