Set the size of the one-dimensional dynamic array

 

Syntax

procedure SetArrayLength(var Arr: <dynamic array>; iNewLength: integer);

 

Parameters and return values

Parameter

Type

Value

Arr

array of...

one-dimensional dynamic array of any type

iNewLength

integer

number of the array elements.

 

Example

The numeration of the array elements is from 0 to iNewLength - 1. The program calculates the number of unique words in the text and displays them in the console:


const
  ORIGINAL_TEXT = 'Take me, I''m alive, never was a girl with a wicked mind'#13#10 +
                  'But everything looks better, when the sun goes down'#13#10 +
                  'I had everything, opportunities for eternity and I'#13#10 +
                  'could belong to the night'#13#10 +
                  'Your eyes, your eyes, I can see in your eyes, your eyes';
var
  sData, s: string;
  iCount, i: integer;
  Arr: array of string;
  bAddFlag: boolean;
begin
  mLogScript(ORIGINAL_TEXT, '');
  sData := ReplaceString(LowerCase(ORIGINAL_TEXT), CRLF, ' ', true, false);
  sData := ReplaceString(sData, ',', '', true, false);
  
  iCount := 0;
  SetArrayLength(Arr, 0);
  
    while length(sData) > 0 do begin
      s := Fetch(sData, ' ');
      bAddFlag := true;
        if iCount > 0 then 
          for i := 0 to iCount - 1 do
            if Arr[i] = s then begin
              bAddFlag := false;
              break;
            end;
            
        if bAddFlag then begin
          inc(iCount);
          SetArrayLength(Arr, iCount);
          Arr[iCount - 1] := s;
        end;
    end;
    
  mLogScript('Unical words list:', '');  
  s := '';
  
    for i := 0 to iCount - 1 do begin
      s := s + Arr[i] + ' ';
      
        if (i + 1) mod 10 = 0 then s := s + CRLF;
    end;  
      
  mLogScript(s, 'Total ' + IntToStr(iCount) + ' words');      
end.


Script work result

[23:57:00] (Log "SetArrrayLength"): Take me, I'm alive, never was a girl with a wicked mind

But everything looks better, when the sun goes down

I had everything, opportunities for eternity and I

could belong to the night

Your eyes, your eyes, I can see in your eyes, your eyes

[23:57:00] (Log "SetArrrayLength"): Unical words list:

[23:57:00] (Log "SetArrrayLength"): [Total 35 words] take me i'm alive never was a girl with wicked

mind but everything looks better when the sun goes down

i had opportunities for eternity and could belong to night

your eyes can see in

[23:57:00] (Run "SetArrrayLength"): Script operation time: 4 ms

[23:57:00] (Run "SetArrrayLength"): Script done successfully.
 

See also

CRLF

Fetch

Inc

IntToStr

Length

LowerCase

mLogScript

ReplaceString

Created with the Personal Edition of HelpNDoc: Free Kindle producer