1
votes

I need to return min and max values of two Integers in many situations in my Pascal Script. But every time I need to create a TStringList, add my two or more Integers to it and convert it to an Array Of String and then get its min and max values using two of my functions called ArrayOfStringMax and ArrayOfStringMin.

I like to have two functions like Min and Max to make this easier like unit Math in Delphi.

For example,

Log(IntToStr(Min(1000, 26)));

Output should be 26.

Log(IntToStr(Max(45, 1989)));

Output should be 1989.

Currently I only need Min and Max for Integer Type. If a function can be made to return minimum and maximum values even of Single, Double, Extended, Int64 types, it will be a very useful function.

UPDATE

procedure StringListToArrayOfString(StringList: TStringList; var ArrayOfString: Array Of String);
var
  X: Integer;
begin
  SetLength( ArrayOfString, StringList.Count);
  for X := 0  to (StringList.Count - 1)  do ArrayOfString[X] := StringList.Strings[X];
end;

function ArrayOfStringMax(ArrayOfString: Array of String): String;
var
  X, M: Integer;
begin
  M := StrToInt(ArrayOfString[Low(ArrayOfString)]);
  for X := 1 to High(ArrayOfString) do
  if StrToInt(ArrayOfString[X]) > M then M := StrToInt(ArrayOfString[X]);
  Result := IntToStr(M);
end;

function ArrayOfStringMin(ArrayOfString: Array of String): String;
var
  X, M: Integer;
begin
  M := StrToInt(ArrayOfString[Low(ArrayOfString)]);
  for X := 1 to High(ArrayOfString) do
  if StrToInt(ArrayOfString[X]) < M then M := StrToInt(ArrayOfString[X]);
  Result := IntToStr(M);
end;

Those are the three functions I currently using in the Script.

Thanks in advance.

1
I cannot imagine that you managed to implement ArrayOfStringMax, yet you are not able to implement a simple Max.Martin Prikryl
I can't understand your comment, Sorry - I am not be able to ...... What?Blueeyes789
You wrote that you already have ArrayOfStringMax, what is a way more complex to implement than Max. So I wonder, how can you have a problem implementing the Max.Martin Prikryl
No. You're wrong..... I can't think how to implement it. I implemented ArrayOfStringMax using a High and Low method.Blueeyes789
So you use the 1000 and 26 and indexes of the array? = You create an array with 1000 elements to find the highest index? I'm afraid your ArrayOfStringMax is either completely wrong or terribly inefficient.Martin Prikryl

1 Answers

1
votes
function Max(A, B: Integer): Integer;
begin
  if A > B then
    Result := A
  else
    Result := B;
end;

function Min(A, B: Integer): Integer;
begin
  if A < B then
    Result := A
  else
    Result := B;
end;

Inno Setup Pascal Script does not support generic function nor function overloading. So if you need to implement the function for floats, you have to use a different name like MinFloat, MaxFloat. The implementation will be identical (except for parameter and return value types obviously).

Though you can share implementation for integer and float types. You can use a LongInt implementation for the Integer type. The same way, you can use a Double implementation for the Single type.


If you want to implement the functions for array of numbers, there's no need to convert the array to strings. Just use array of numbers, array of Integer:

function Max(N: array of Integer): Integer;
var
  I: Integer;
begin
  if GetArrayLength(N) = 0 then RaiseException('Array is empty');

  Result := N[Low(N)];
  for I := Low(N) + 1 to High(N) do
  begin
    if N[I] > Result then Result := N[I];
  end;
end;