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

Alexander Konovalov alexander.konovalov at gmail.com
Tue Feb 14 10:08:24 GMT 2012


Dear Aasma Shaheen,

On 13 Feb 2012, at 10:31, Aasma Shaheen wrote:

> 
> Sir problem is that when i want to call several loops from a single file containg alot of loops ( i have attached one as example) then it dosent help.The way i have talked reads only if file contains 1 and only 1 loop.but it takes alot of time to copy them in different files (i-e make 2764 files),and then call them in GAP.Isn't there anty shortcut way to call them from a single file containing them.    <order12.txt>

Thank you for another example. It nicely illustrates that there is no a notion of a standard text file with space separated integers that GAP should somehow read in an efficient manner. In your first post in the forum your example looked like

=======
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
2 3 1 0 6 10 11 5 9 13 7 4 8 12
3 2 0 1 11 7 4 10 12 8 5 6 13 9
4 5 6 11 1 0 12 3 13 10 2 8 9 7
5 4 10 7 0 1 2 13 11 12 9 3 6 8
6 8 11 4 12 2 13 0 10 1 3 9 7 5
7 9 5 10 3 13 0 12 1 11 8 2 4 6
8 6 9 12 13 11 10 1 3 0 4 7 5 2
9 7 13 8 10 12 1 11 0 2 6 5 3 4
10 12 7 5 2 9 3 8 4 6 13 0 11 1
11 13 4 6 8 3 9 2 7 5 0 12 1 10
12 10 8 13 9 6 7 4 5 3 11 1 2 0
13 11 12 9 7 8 5 6 2 4 1 10 0 3


2

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
2 3 1 0 6 10 7 5 11 13 9 4 8 12
3 2 0 1 11 7 4 6 12 10 5 8 13 9
4 5 6 11 1 0 12 3 13 8 2 10 9 7
5 4 10 7 0 1 2 13 9 12 11 3 6 8
6 8 7 4 12 2 3 0 10 1 13 9 11 5
7 9 5 6 3 13 0 2 1 11 8 12 4 10
8 6 11 12 13 9 10 1 4 0 3 7 5 2
9 7 13 10 8 12 1 11 0 5 6 2 3 4
10 12 9 5 2 11 13 8 3 6 4 0 7 1
11 13 4 8 10 3 9 12 7 2 0 5 1 6
12 10 8 13 9 6 11 4 5 3 7 1 2 0
13 11 12 9 7 8 5 10 2 4 1 6 0 3

...
=======

and the attached file order12.txt has slightly different format - only 12x12 blocks, separated by empty lines. 

=======
0 1 2 3 4 5 6 7 8 9 10 11 
1 0 3 2 5 4 7 6 10 11 8 9 
2 3 0 1 6 8 4 11 5 10 9 7 
3 2 1 0 10 11 8 9 6 7 4 5 
4 5 9 10 1 0 11 2 7 8 6 3 
5 4 7 11 0 1 10 8 9 2 3 6 
6 7 10 8 9 2 1 0 11 3 5 4 
7 6 5 9 11 10 0 1 3 4 2 8 
8 10 11 6 2 9 5 3 4 0 7 1 
9 11 4 7 8 6 3 10 0 5 1 2 
10 8 6 4 7 3 9 5 2 1 11 0 
11 9 8 5 3 7 2 4 1 6 0 10 

0 1 2 3 4 5 6 7 8 9 10 11 
1 0 3 2 5 4 7 6 10 11 8 9 
2 3 0 1 6 8 4 11 5 10 9 7 
3 2 1 0 10 11 9 8 7 6 4 5 
4 5 6 11 0 1 2 10 9 8 7 3 
5 4 8 10 1 0 11 9 2 7 3 6 
6 7 4 9 2 10 0 1 11 3 5 8 
7 6 10 8 11 9 1 0 3 5 2 4 
8 11 5 7 9 2 10 3 0 4 6 1 
9 10 11 6 8 7 3 5 4 0 1 2 
10 9 7 5 3 6 8 4 1 2 11 0 
11 8 9 4 7 3 5 2 6 1 0 10 

...
=======

However, the solution is quite straightforward: take the function 
suggested earlier in this thread by Alexander Hulpke and adjust it
to your needs:

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;

What you need to do is to catch the case if the line is empty,
whenever it's just a new line or it has some spaces before CR/LF.
For this, analyse what SplitString returns before starting a 
loop over its output. The two cases below show what will happen
if the line is empty:
 
gap> l:="\n";    
"\n"
gap> l:=Chomp(l);
""
gap> SplitString(l," ,");
[  ]

gap> l:="     \n";
"     \n"
gap> l:=Chomp(l); 
"     "
gap> SplitString(l," ,");
[ "", "", "", "", "" ]

Then, instead of accumulating all lines in the resulting list a,
you need to gather lines/integers from one block (table) together.

I do not know if you want to return a list of 2764 loops, or 
you want to read them one by one and do something for each single
loop in a loop, so I will leave the final implementation to you.
If you will have further questions, please don't hesitate to ask.

hope this helps,
  Alexander




More information about the Forum mailing list