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

It is, and eldee didn’t mean that. They meant that you tried to insert an entire 10-element Vector{Int} as 1 element of another Vector{Int}, which is not possible because Vector{Int} can only have elements of Int. You would need a Vector{Vector{Int}} to do that exact thing, though there are likely better ways to cache each sequence of integers for each line of text, depending on how file.txt is structured.

There is no separate 1 and 0 there, it really is 10 fitting in one element (we don’t call it cell, but that’s what it is). If you’re wondering why 10 is 2 characters taking twice the space of the smaller integers, it’s just the monospaced printout. A character has nothing to do with the size of the element, and monospacing will never squeeze multiple characters into the space of one.

mbauman is talking about types, specifically concrete types that can have instances. For example, "hello" and "world" are instances of the concrete type String. For this question, we need to consider 2 purposes of a concrete type:

  1. The computer doesn’t directly store the instances "hello", 7, or 3.14. There are physical media storing binary data, which we can call 1s and 0s. A concrete type specifies how a segment or multiple segments of binary data represents instances, and users usually don’t need to care about the specifics.

  2. A type helps specify what functions/methods can work on its instances. Users do have to know what types can do, and convert between types to do what they want.

As mbauman said, the String type can represent integers, and its binary data and thus the integer can get as large as your memory allows. The problem is it is not a type suited for arithmetic, so there aren’t methods implemented for it, e.g. "1" + "1" will fail. There are however many numeric types like Int8, Int16, Int32, Int64, etc that do support arithmetic, and parse can be used to convert Strings to them, as demonstrated earlier. Numeric types also often have fixed sizes of binary data, so they can support the function typemax, which tells you a type’s maximum representable value:

julia> typemax(Int64)
9223372036854775807

If your array’s element type happens to be one of these types, you can extract it with eltype and pass it to typemax:

julia> typemax(eltype(fill(0, 10)))
9223372036854775807

Note that Int (and UInt) isn’t the same data type everywhere; it aliases Int32 on 32-bit systems but Int64 on 64-bit systems, because of the differences of how they work. As a user, you don’t have to care about all the details, but you should use Int for array dimensions/indexing and recognize that the parser will treat integers in text as Int (so validate and convert values if you need). You can find out which system via Julia by evaluating Int in the REPL and seeing the printout, or Sys.WORD_SIZE encodes it directly.

2 Likes

Hello,
Let’s assume the contents of the file file.txt are as follows:

9 10

Is it possible for you to explain the following code step by step?

    array = fill(0,10)
    counter = 1
    for line in eachline("file.txt")
        if line != ' '
            array[counter] = parse.(Int, split(line))
            counter +=1
        end
    end

I’m not sure what to explain, can I ask what the goal is? That code is known not to work and the error has been explained, so I don’t know what else about it you want to know, and if you want to see step-by-step values, you can use the REPL to evaluate the lines. Well, you would have to break the for loop into separate iterate calls if you’re not using a debugger, though you’d hit the error on the first iteration anyway.

1 Like

Hi,
Thanks again.
How can I fix the problem with the following code without using additional, advanced functions?

function arrayandfile()
    array = fill(0,10)
    counter = 1
    for line in eachline("file.txt")
        if line != ' '
            array[counter] = parse.(Int, split(line))
            counter +=1
        end
    end
    return(array)
end
println(arrayandfile())

I’m not sure why this does not fulfil your criteria…

1 Like

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()