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

Alexander Konovalov alexander.konovalov at gmail.com
Thu Feb 9 21:09:46 GMT 2012


Dear Lisette, dear GAP Forum,

the file with integers is not a precise description of a standard format
(what is the role of empty lines? how to specify comments? what to do if 
there is a text there? How to convert strings to GAP objects? Are there
line breaks? etc.) and it is not recognised a standard GAP input file. 

To the contrary, a proper GAP input file can be read just by a single 
command like, for example,

	Read("myfile.g");

It is often not difficult to convert a file into a GAP input file (by 
inserting a few brackets and commas). Also, in some cases it may be 
possible to slightly adapt the program which is used to produce the 
file in a way that it will generate GAP input.

On the other hand, it may be not so hard to customise the code of a
function like ReadIntegerListsFromFile below, or write another custom 
piece of code (e.g. to read only one word at a time) to read user's 
data files if they have some appropriate form.

For not too large files, one may also use another function suggested
to us by Stefan Kohl: it reads the whole file at once, breaks it into 
strings, and then breaks strings into further pieces:

ReadIntegerListsFromFileShort :=
 file -> List(SplitString(StringFile(file),"\n"),
              line->List(SplitString(line," ,"," \r\n"),Int));

For very large files, however, ReadIntegerListsFromFile will work better 
since it does not create a very long string in the middle of computation.

Note that both approaches also allow to read CSV files (comma-separated 
values). However, different applications may want to represent CSV files 
in different forms. For example, another function to read CSV files will 
appear in the next release of GAP: it will represent the content of the 
CSV file as a list of records taking component names from the 1st line 
of the CSV file. 

Finally, you may place the code to create all functions that you need,
load all necessary packages, etc. in the .gaprc file (see ?.gaprc) in 
your home directory. This file will be read during the startup of GAP 
automatically, so they will be available to you straight from the start. 
(note that in the next GAP release, '.gaprc' file will be superseded by 
'~/.gap/gap.ini' and 'gaprc' files, see the manual of GAP 4.5.2(beta) 
for details).


Hope this may be useful,
Alexander,

on behalf of the GAP Support Group



On 8 Feb 2012, at 20:48, Lisette Brillemans wrote:

> Gap is a very user friendly program. To read  a file with integers, the only thing you need 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;
> 
> 
> So easy to remember....
> 
> Lisette
> 
> 
> _______________________________________________
> Forum mailing list
> Forum at mail.gap-system.org
> http://mail.gap-system.org/mailman/listinfo/forum




More information about the Forum mailing list