[GAP Forum] A strange feature of AppendTo

Frank Lübeck Frank.Luebeck at math.rwth-aachen.de
Thu Nov 23 11:46:35 GMT 2006


On Thu, Nov 23, 2006 at 11:23:25AM +0100, Mathieu Dutour wrote:
> this is something rather mundane, but when I do
> the following 
> 
> output:=OutputTextFile("test", true);;
> AppendTo(output, TheString);
> CloseStream(output)
> 
> then some arbitrary "\n" are inserted after 80
> characters. Is there a reason for that?
> 
>   Mathieu
Dear Mathieu, dear Forum,

When you use Print in GAP it uses some algorithm to break the
output into lines (in the hope to make it more readable):

gap> Concatenation([1],[1..35]); 
[ 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
  21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 ]
gap> TheString := Concatenation(List([1..10],i->"some string"));
"some stringsome stringsome stringsome stringsome stringsome stringsome string\
some stringsome stringsome string"
gap> Print(TheString,"\n");  
some stringsome stringsome stringsome stringsome stringsome stringsome strings\
ome stringsome stringsome string

The same algorithm is used if you print to an OutputTextFile stream. This
is what you describe. 

If you don't want this formatting you can switch it off as follows:

gap> output:=OutputTextFile("test", true);;
gap> SetPrintFormattingStatus(output, false);
gap> AppendTo(output, TheString);            
gap> CloseStream(output);

Now there is no additional '\n' and this is faster as well. 

Note also that there is a function FileString:

gap> FileString("test",TheString,true);;

(This and a corresponding StringFile are quite efficient for writing and
reading large strings to/from files.)

Best regards,

  Frank

-- 
///  Dr. Frank Lübeck, Lehrstuhl D für Mathematik, Templergraben 64,  ///
\\\                    52062 Aachen, Germany                          \\\
///  E-mail: Frank.Luebeck at Math.RWTH-Aachen.De                        ///
\\\  WWW:    http://www.math.rwth-aachen.de/~Frank.Luebeck/           \\\





More information about the Forum mailing list