[GAP Forum] Re: Reading data

Alexander Hulpke hulpke at math.colostate.edu
Wed Aug 4 03:31:16 BST 2004

Dear GAP Forum,

Tim Boykett wrote

>    this should be a silly question: how do I read in
> some table of data? I have a file with what is essentially
> a matrix as space-separated integers, and I would like
> to get it into GAP as a matrix without having to manually
> place commas and square brackets in the file.

For the case that the data format of the file is both simple and reliable,
and that the objects in the file can be understood by GAP
--as is the case for a file containing space separated integers--
the following is perhaps the easiest way to import the data.

1. Read the file using `StringFile' (see ``StringFile'' in the GAP
   documentation); this yields a GAP string.

2. Perform the manipulations you do not want to apply to the file.
   In the example, normalize the whitespace if necessary
   (see ``NormalizeWhitespace'' in the GAP documentation),
   globally replace the whitespace by commas and --if applicable--
   replace line separators by substrings of the form `],[' representing
   the switch to a new matrix row (see ``ReplacedString''),
   and add square brackets around the string (see ``Concatenation''),
   such that the string finally has a syntax that GAP understands.

3. Evaluate this string using `EvalString' (see ``EvalString'');
   this will return the matrix you wanted to get.

   If the input data does not contain information about the distribution
   of the entries to rows and columns then the result of step 2. may be
   a list of all matrix entries.
   Then you can turn this into a matrix using a command such as

      List( [ 1 .. nrrows ], i -> list{ [ 1 .. nrcols ] + (i-1)*nrcols } );

   where `list' is the list of integers, and `nrrows' and `nrcols'
   are the matrix dimensions.

If the data format is more complicated or not reliable
--for example, the numbers of rows and columns of the matrix may be stored
in the header of the file, or some missing matrix entries may be marked
as `?' or something similar that is not understood by GAP--
then you can read in a text file via streams:

   while not IsEndOfStream(stream) do

     ... process


Note that essentially you will be getting strings
and will have to cut them apart yourself (for example using `SplitString').
You then can use `Int' or `Rat' to convert a string to a number,
and you can catch weird input before GAP signals an error.
Also be aware that the strings you get are CR terminated and you might want
to clean them up using `Chomp'.

All the best,
Thomas Breuer and Alexander Hulpke

More information about the Forum mailing list