[GAP Forum] question about strings

Frank Lübeck Frank.Luebeck at math.rwth-aachen.de
Thu May 24 21:45:53 BST 2007


On Thu, May 24, 2007 at 04:47:25PM +0100, Joao Araujo wrote:
> I use some programs to produce a file, say text.txt with content
> 
> This is
> the
> file.
> 
> and I want to use GAP to manipulate it. In order to do that I 
> open the file and write
> 
> u:="This is
> the
> file.";;
> 
> Then I save the file and run
> 
> perl -e 'while (<>) { if (! /\|$/ ) { chomp; } print ;}' text.txt > text.out
> 
> in order to remove line breaks.

Dear Joao, dear Forum,

Actually, GAP can deal quite efficiently with some sort of simple file
manipulations via GAP strings. See the help section 

?Reference: Operations to Produce or Manipulate Strings

For example: For reading the content of an arbitrary file into a GAP string
you can use 'StringFile' (and 'FileString' for writing to a file).

For manipulations of the string functions like 'SplitString',
'JoinStringsWithSeparator', 'NormalizeWhitespace', 'PositionSublist',
'ReplacedString', 'RemoveCharacters' (I just noticed that this is missing in
the documentation, will be added with the next update) can be useful. All of
them work quite well with huge strings/files.

So, take your file text.txt as produced by your other programs:

This is
the
file.

Then in GAP say

gap> u := StringFile("text.txt");
"This is\nthe\nfile.\n\n"
gap> RemoveCharacters(u, "\n"); u;
"This isthefile."
gap> u := StringFile("text.txt"); 
"This is\nthe\nfile.\n\n"
gap> NormalizeWhitespace(u);  u;
"This is the file."

With 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