reset array

mezura
mezura used Ask the Experts™
on
how can i reset an array? i have a declaration

temparray: array[0..1000] of strings;

i tried temparray:=nil; but it gives me an error of incompatible types.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
VGR

Commented:
for i:=0 to 1000 Do temparray[i]='';
Commented:
Or, slightly better programmatically:

for i := Low(temparray) to High(temparray) do temparray[i] := '';

Doesn't rely on frowned-upon magic numbers that way.

Geoff M.
VGR

Commented:
quoting : temparray: array[0..1000] of strings;
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

VGR

Commented:
no magic...
VGR

Commented:
Const cMax = 1000;

Var temparray: array[0..cMax] of strings;
  i : LongWord;

// later
for i:=0 to cMax Do temparray[i]:='';

Commented:
Use dynamic arrays. Make:

var
  Ar: array of strings;

procedure InitProc;
begin
  SetLength(Ar, 0);
  SetLength(Ar, 1000);
end;
wkz

Commented:
temparray := nil is a bad, bad idea
i assume you want to free up memory space or something? with "temparray: array[0..1000] of strings;", the closest you can do is all the above answers (temparray[i]='';)

try Noodlesf's method (Ar: array of strings;  &  SetLength(Ar, 0);) for "cleanest" results
VGR

Commented:
slowest

the only advantage is that the heap allows for bigger arrays being allocated dynamically, but it's sloooowwwww
VGR

Commented:
BTW, for a normal compiler, computing High() at each iteration to test i against it adds a lot of overhead. Who wrote "better programmatically" ? ;-)

Commented:
I believe "for" loops only do one calculation before entering the loop and the results are stored for faster looping. So High is only tested once, returning 1000 or whatever the number may be, and that number is stored locally for a quick test next time round the loop. So... I did!

Geoff M.
VGR

Commented:
I don't think so, because the loop can (althought it's not recommended) manipulate the counter variable and thus the "exit condition"

Commented:
From Delphi help:

"It is illegal to assign a value to the for loop control variable inside the for loop.
If the purpose is to leave the loop prematurely, use a break or goto statement. "

Geoff M.
VGR

Commented:
he he he

is Delphi your only reference language ? ;-)

Commented:
I think even my cats would be able to tell you something simple like that ;-)
VGR

Commented:
Okey, I reckon it's not feasible in a compiled language like Delphi (althought it does compile and execute ;-)

program loopmanip;

{$APPTYPE CONSOLE}

Uses SysUtils;

Var temparray : Array of String;
    i         : Word;
Begin
  SetLength(temparray,4); // or 1000 ;-))
  For i := Low(temparray) to High(temparray) do
   Begin
     WriteLn('temparray['+IntToStr(i)+'] will become empty');
     temparray[i] := '';
     if i=2 Then SetLength(temparray,6); // change dimension
   End; // For
  ReadLn;
End.


I would be interested to see what will happen in a (pseudo-) compiled PHP version, because the following works like in BASIC (ie, the loop will go one step further than initially computed ;-)

<?php
$temparray=array('aa','bb','cc','dd'); // not 1000 but 4 strings

for ($i=0;$i<count($temparray);$i++) {
 echo "iteration $i : '{$temparray[$i]}' becomes empty<BR>";
 $temparray[$i] = '';
 if ($i==1) $temparray[]='newstring'; // add a new element
} // for
?>

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial