Forum Moderators: phranque

Message Too Old, No Replies

Incrementing a Variable Name within a for loop

Is it possible...?

         

BlobFisk

7:30 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I was wondering if it is possible to increment a variable name within a for loop.

For example:


for RecNo = 1 to HatsLookup.Count
Do stuff
Do more stuff
Create a varable called loop & RecNo
next

The variable would be generated at every pass through the loop: loop1, then loop2..... loop(RecNo). Is this possible at all?

TIA

aspdaddy

8:22 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



no, not in any scripting language i know :)
just use an array:
loop[RecNo]

BlobFisk

11:52 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, I was guessing that an Array was the way to go. I couldn't think of another way to do it and was just wondering if anyone else had seen or done it!

Thanks!

rpking

1:17 pm on Aug 22, 2002 (gmt 0)

10+ Year Member



How about PHP's variable variables [phpbuilder.com]? They sound similar to what you need...

jatar_k

4:10 pm on Aug 22, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



rpking has it

you can do something like

for ($i = 0; $i < $num_cols; $i++)
{
${"column$i"} = array();
}

this increments the array name so you end up with three arrays $column1, $column2 and $column3.

BlobFisk

8:11 am on Aug 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks a million all.

I've never used PHP... looks like it's time to have a look at it!

andreasfriedrich

10:32 am on Aug 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do the same in perl using symbolic references. The code in msg#5 will work in perl as well except for the language construct array().

Furthermore you could achieve that effect with any other scripting language supporting some kind of eval function. Although I donīt really see why you donīt go for arrays since they are made for exactly that purpose.

BlobFisk

10:47 am on Aug 23, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're right, Arrays are exactly the way to go and I've started working on this. I figured that an eval function to construct the variable name was the path to take.

Thanks for the help!