[GAP Forum] Correction: How to read loops from 1 file.

Alexander Hulpke ahulpke at gmail.com
Wed Feb 8 20:17:11 GMT 2012


Dear Forum,

It was pointed out that I should close the stream at the end of the function to avoid error messages if running the function multiple times, so the correct version is:

ReadIntegerListsFromFile:=function(file)
local l,f,i,a,r;
  f:=InputTextFile(file);
  a:=[];
  while not IsEndOfStream(f) do
    l:=ReadLine(f);
    if l<>fail then
      l:=Chomp(l); # remove trailing CR/LF
      r:=[];
      for i in SplitString(l," ,") do # separate by SPACE or ,
        if Length(i)>0 then
          Add(r,Int(i));
        fi;
      od;
      Add(a,r);
    fi;
  od;
  CloseStream(f);    # <== New line
  return a;
end;

Best,

   Alexander


More information about the Forum mailing list