ERROR: LoadError: BoundsError: attempt to access 10-element Vector{Int64} at index [11]

Looping over things one by one is a fine place to start if you find it easy to understand. The only issue with your original code was that it was looping over individual characters like '1', ' ', '2', '\n' (new line), and so on, not strings like "1", "10". See if this function makes sense to you. And happy learning, it’ll all make more sense soon enough.

function arrayandfile()
    # Create a collection of ten zeros as a placeholder.
    array = fill(0,10)
    counter = 1
    # Get each line in the file as a string like "1 2 10".
    for line in eachline("file.txt")
        # Split the line into a collection of pieces like ["1", "2", "10"] without spaces.
        pieces = split(line)
        # Get each piece like "1" or "10" from the collection.
        for piece in pieces
            # Transform the piece into an actual number (type Int, integer).
            integer = parse(Int, piece)
            # And insert it into the array at the position given by the counter.
            array[counter] = integer
            # Increment the counter.
            counter += 1
        end
    end
    return array
end

arrayandfile()
2 Likes