3
votes

I've been working on a specific piece of code, which simultaneously reads from two files and compares data. Something in the lines of:

for l1 in eachline (firstfile)
    for l2 in eachline (secondfile)

        if l1==l2
        println("match!");
        end
    end
end

Yet what happens is the following> iteration proceeds only for first line of first file and all lines in second file, but then stops. So instead of using the second line of first file in the next cycle, program stops without errors. How is this done in Julia if not with the following snippet?

Thank you.

1

1 Answers

3
votes

Each file has a pointer to the current read location. After reading a file completely, you need to reset the read location to the beginning of the file in order to re-read it. This is what the extra seek line in the code below does:

for l1 in eachline(firstfile)
    for l2 in eachline(secondfile)
        if l1==l2
            println("match!");
        end
    end
    ### ADD THE FOLLOWING LINE
    seek(secondfile,0)
end