Board index » delphi » Know anything about text arrays?

Know anything about text arrays?

I'm trying to write a procedure that will take a string and add it on to the
end of an text array. If the array was named array, and the string named
string, then I'm trying to use a statement like "array = array + string".
I've found that this obviously does not work, and looking this up in the "text
book" they gave us is next to impossible. Please will some kind hearted person
e-mail me with an idea or two on how to accomplish this task. I would
appreciate it greatly.
Thanks!

--
 - Jason Clarke
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
"We in this country, in this generation, are - by destiny rather than choice -
the watchman on the walls of world freedom. We ask, therefore, that we may be
worthy of our power and responsibility, that we may exercise our strength with
wisdom and restraint, and that we may achieve in our time and for all time the
ancient vision of peace on earth, goodwill toward men. That must always be our
goal, and the righteousness of our cause must always underlie our strength. For
it was written long ago, except the Lord keep the city, the watchman waketh in
vain."
        -Remarks prepared by President Kennedy for delivery at the Dallas Trade
Mart

 

Re:Know anything about text arrays?


Quote
> I'm trying to write a procedure that will take a string and add it on to
the
> end of an text array. If the array was named array, and the string named
> string, then I'm trying to use a statement like "array = array + string".
> I've found that this obviously does not work, and looking this up in the
"text
> book" they gave us is next to impossible. Please will some kind hearted
person
> e-mail me with an idea or two on how to accomplish this task. I would
> appreciate it greatly.
> Thanks!

No easy solution i'm afraid.

You can do like this:

...

Type
   TArray = array[1..10] of string;

Var
  aArray : TArray;
   str : string;

{ Empty the array: }
FillChar(aArray, sizeof(TArray), #0);

{ When inserting into the array, check to see if string in array equals
nothing: }
i = 1;
while aArray[i] = '' do
   inc(i);
aArray[i] := str;

...

You can also have a variable telling you how far you have gotten in the
array, starting from 1.

This is one solution. Keep an eye for others, and good luck.

--
Brian Pedersen, System Specialist, Alta Copenhagen
http://www.alta.dk
Personal homepage:
http://home6.inet.tele.dk/brianp

Re:Know anything about text arrays?


Quote
cla...@intergate.bc.ca (Jason Clarke) wrote:
>I'm trying to write a procedure that will take a string and add it on to the
>end of an text array. If the array was named array, and the string named
>string, then I'm trying to use a statement like "array = array + string".
>I've found that this obviously does not work, and looking this up in the "text
>book" they gave us is next to impossible. Please will some kind hearted person
>e-mail me with an idea or two on how to accomplish this task. I would
>appreciate it greatly.
>Thanks!
>--
> - Jason Clarke
>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The user TYPEs may be passed as VAR parameters to a procedure if
needed.

Program ArrayExtend;
{ Turbo v6.0   <clifp...@airmail.net>  Aug 1, 1997
This shows an easy way to do what you want. If you wish to store array
elements somewhat randomly (instead of serially as here), just keep
up with the array index.

Should you run out of stack memory for the array, you can use an array
of pointers instead of s80.  The much shorter pointers point to the
memory locations (in the heap) that store the strings. }

CONST  ArrayMax = 5;  {for test purposes. Increase as needed}

TYPE
range = 0..ArrayMax;  {indx = 0 is used to signal quit.
                       a[0] is used as an input buffer.}
s80 = String[80];
arry = Array[range] of s80;

VAR
a:arry;
n, indx:range;  {positive integers from 0 to ArrayMax}
done:Boolean;

BEGIN
     Writeln; Writeln;
Repeat
     indx := 0;
     Writeln('Terminate by just pressing <Enter>');
     Repeat
          Writeln('Enter a line of text below:') ;
          Readln( a[0] );
          If a[0] <> '' then   {store in next array element}
          Begin
               Inc(indx);
               a[indx] := a[0];
          End;
          done := (a[0] = '') OR (indx = ArrayMax);
          Writeln;
     Until done;

     If not (indx = 0) then  {display array contents}
     Begin
          Writeln;
          If indx = ArrayMax then Writeln('ARRAY FULL');
          Writeln('In order of entry, the array stores: ');
          Writeln;
          For n := 1 to indx Do Writeln(a[n]);
          Writeln;
     End;
Until indx = 0;
END.

Other Threads