0

I'm trying so hard to initialize an array or arrayList of strings from a file while using a loop, but every function I'm using- put/enter/force nothing seems to work. the array time after time got filled with the last string I read even though I'm accessing a specific index that I increasing every iteration. (I tried to add regular constant string and it worked well, I don't understand the difference.

Thanks to everyone who would help.

tArray:ARRAY[STRING] -- declaring
create tArray.make_empty

readingFile() --function
local
    k:INTEGER_32
do
    from k:=0
    until curFile.end_of_file
    loop
        curFile.read_line
        curLine:=curFile.last_string
        tArray.force (curLine, k)
        --tArray.put(curLine, k)
        --tArray.enter (curLine, k)
        --tArray.at (k):=curLine
        --tArray.force ("sara", k+1)
        k:=k+1
    end
end
  • I think that your problem is that you always put the same string in the list. When you do a `curFile.read_line`, it change the `curFile.last_string` object but do not create a new STRING instance. Try to do `tArray.force (curLine.twin, k)` instead of just `tArray.force (curLine, k)`. – Louis M Jun 05 '18 at 17:41
  • thank you! worked with tArray.force (curLine.out, k), I have an old version of the Eiffel Studio. – Noy Moshkovitz Jun 05 '18 at 17:52

1 Answers1

1

The feature read_line does not create a new string object every time, but rather reuses the last one. In other words, last_string always refers to the same object. The solution is to use a clone of the object associated with last_string at every iteration:

curLine := curFile.last_string.twin
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35