mezura
asked on
reset array
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.
temparray: array[0..1000] of strings;
i tried temparray:=nil; but it gives me an error of incompatible types.
for i:=0 to 1000 Do temparray[i]='';
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
quoting : temparray: array[0..1000] of strings;
no magic...
Const cMax = 1000;
Var temparray: array[0..cMax] of strings;
i : LongWord;
// later
for i:=0 to cMax Do temparray[i]:='';
Var temparray: array[0..cMax] of strings;
i : LongWord;
// later
for i:=0 to cMax Do temparray[i]:='';
Use dynamic arrays. Make:
var
Ar: array of strings;
procedure InitProc;
begin
SetLength(Ar, 0);
SetLength(Ar, 1000);
end;
var
Ar: array of strings;
procedure InitProc;
begin
SetLength(Ar, 0);
SetLength(Ar, 1000);
end;
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
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
slowest
the only advantage is that the heap allows for bigger arrays being allocated dynamically, but it's sloooowwwww
the only advantage is that the heap allows for bigger arrays being allocated dynamically, but it's sloooowwwww
BTW, for a normal compiler, computing High() at each iteration to test i against it adds a lot of overhead. Who wrote "better programmatically" ? ;-)
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.
Geoff M.
I don't think so, because the loop can (althought it's not recommended) manipulate the counter variable and thus the "exit condition"
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.
"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.
he he he
is Delphi your only reference language ? ;-)
is Delphi your only reference language ? ;-)
I think even my cats would be able to tell you something simple like that ;-)
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['+IntTo Str(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
?>
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['+IntTo
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'
for ($i=0;$i<count($temparray)
echo "iteration $i : '{$temparray[$i]}' becomes empty<BR>";
$temparray[$i] = '';
if ($i==1) $temparray[]='newstring'; // add a new element
} // for
?>