Jump to content
FailedFace

Looping through variables

Recommended Posts

I've been looking into finding a way to loop through variables via a counter:

var v1,v2,v3,v4,v,i:integer;

 

begin

v1:=1;

v2:=2;

v3:=3;

v4:=4;

 

repeat

writeln(v<i>);

i:=i+1;

until (i=4)

end.

 

So i want to be able to print all of my variables v1 -> v6 via a loop. Is there any way to assign var v to be of type int that takes the place of other vars? (to allow my adding of numbers at the end of the variable to call another variable)

 

:D

 

Thank for any help i get even if it is a no. Not that it will stop me from trying. :P

Link to comment
Share on other sites

Wouldn't an array better suit your purpose?

program New;

var
 v: TIntegerArray;
 i: Integer;

begin
 SetLength(v, 4);
 v[0] := 1;
 v[1] := 2;
 v[2] := 3;
 v[3] := 4;

 for i := 0 to High(v) do
   writeln(IntToStr(v[i]));
end.

Or alternately, if you were intent on keeping the number of the vars as 1 through 4:

program New;

var
 v: array [1..4] of Integer;
 i: Integer;

begin
 v[1] := 1;
 v[2] := 2;
 v[3] := 3;
 v[4] := 4;

 for i := Low(v) to High(v) do
   writeln(IntToStr(v[i]));
end.

Edited by Bixby Sayz
Link to comment
Share on other sites

Yeah an array will do just fine. I assume there are vectors too. Was trying a PHP approach that uses the counter of a for loop to set variables to different values for each iteration by adding an extension on the variable name. I didn't even bother to check that arrays were supported, not that it would have been special for arrays to exist with SCAR, just had a momentary laps in judgement for what data structures I should have been using. Not sure why I did that. Thanks Bixby and Wanted, that was a quick and useful response.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...