Avoiding side effects



Another thread about functions and side effects triggered me to
ask advice how to solve a specific problem without using side effects.

Problem:
Two files A and B containing one code per line (actually a
material number). I want to have the difference between those
files (A-B), i.e. remove those lines in A that are mentioned in
B.
B may contain codes that are not present in A.
A and B contain up to 1.5 million lines each.

My solution:
I sort the files alphabetically and remove possible duplicates
(Unix commands sort and uniq).
I create two functions, Strip and Advance.
function Strip (Remove_This : in String) return String
-- Displays rows until row > Remove_This.
function Advance (Candidate : in String) return String
-- Advance in file until row > Candidate.
The main program is then merely a loop where I alternate between
advancing lines in the two files until the row in the file is
greater than the value to compare against:
....
loop
Trans_String := To_Unbounded_String (Strip (To_String
(Trans_String)));
Trans_String := To_Unbounded_String (Advance (To_String
(Trans_String)));
end loop;
The problem is how to deal with the situation when I reach the
end of the file.
I have done it using a side effect:
EOF_Reached : Boolean := False;
loop
Trans_String...
exit when EOF_Reached;
Trans_String...
exit when EOF_Reached;
end loop;
-- Display possible tail of file A.
EOF_Reached is set to True in the functions Strip and Advance as
a side effect.

Is there a better way without using the side effect EOF_Reached?

--
Anders
.



Relevant Pages

  • Re: Get the path and namefile in run time
    ... function Get_Path_Only return String; ... -- This returns the first N characters of the program name. ... end loop; ...
    (comp.lang.ada)
  • Re: Parse String
    ... it picking up substrings beginning with 'f' followed by hyphens, ... Function gnaf(s As String) As Variant ... Exit Do 'inner Do ... Loop ...
    (microsoft.public.excel.programming)
  • Re: Newbie needs help
    ... You want to exit the loop when the user types ... void ParseLine (const string& cInput, ...
    (comp.lang.cpp)
  • Re: Ada Shootout program for K-Nucleotide (patches)
    ... - a loop reading input lines into an ... ubounded string replaces the recursive String "&"ing ... The following Getline has left me unconvinced. ... exit when Last = Item'Last; ...
    (comp.lang.ada)
  • Re: new line character in a string
    ... is there any other mechanism to use newline character in a PS string? ... {%loop over file ... dup currentfile exch readstring exch ...
    (comp.lang.postscript)