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

Alexander Hulpke ahulpke at gmail.com
Wed Feb 8 16:25:50 GMT 2012


Dear Forum,

>        I wanna ask how can we read loops in GAP from a single file which
> contains a number of loops in sequence.

As this is a question that might be of general interest I  appended is a short function `ReadIntegerListsFromFile' that takes
as input a file name for a text file (e.g. .txt, not .doc) and reads rows of integers, putting each row in a list of entries (assuming that integers are separated by space or comma, and that there are no missing entries. I hope it also can serve as a template for other file reading tasks.

For example for your file:

> 1
> 
> 0 1 3 2 5 4 7 6 9 8 11 10 13 12
> 
> 0 1 2 3 4 5 6 7 8 9 10 11 12 13
> 1 0 3 2 5 4 8 9 6 7 12 13 10 11
[...]
the function returns a list that starts:
[ [ 1 ], [  ], [ 0, 1, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12 ], [  ], 
  [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ], 

From this it is not hard to write a GAP function which now parses the output in the desired form for the respective application, e.g. to interpret chunks of the list as multiplication tables of loops.

Note that -- unless the file is in the current working directory for GAP -- you must give a full path. Typical full paths for files in the `Desktop' folder are:
"/Users/xyrxmir/Documents/gapstuff/mydata.txt" (OSX) or
"/cygdrive/c/Documents and Settings/Administrator/Desktop/mydata.txt" (Windows, English version).

Best wishes,

   Alexander Hulpke

===== The function:

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;
  return a;
end;

======

-- Colorado State University, Department of Mathematics,
Weber Building, 1874 Campus Delivery, Fort Collins, CO 80523-1874, USA
email: hulpke at math.colostate.edu, Phone: ++1-970-4914288
http://www.math.colostate.edu/~hulpke





More information about the Forum mailing list