Create variables with a loop?

January 25, 2006 / Filed under: Programming

Let’s say I want to quickly create five new variables. It doesn’t matter what programming language we use.

Let’s use JavaScript, just for this example.

Instead of doing this:

var new_variable_1;
var new_variable_2;
var new_variable_3;
var new_variable_4;
var new_variable_5;

... I’d like to "group" this better, by doing something like this:

for (i=1; i<6; i++)
{
    var new_variable_ + i;
}

See what I’m trying to do? By using the temporary variable i, I should be able to append that variable to the end of each variable I am creating. Make sense?

The problem is I’m not sure if I can add a variable value to a variable name.

Ahhh, only after you spend hours doing this crap, do you come up with such strange concoctions.

It’s not even 5 PM, and already I need a beer.

Comments/Mentions

# Michael at 1/25/2006 2:37 pm cst

You should be able to do that. I needed to for one of the java things I was doing.

# Josh at 1/25/2006 4:22 pm cst

Sounds kinda-array-ish. Can't you just create the array then fill it up with as much stuff as you want? Or was that just the example that I kinda misconstrued?

# Michael at 1/25/2006 5:16 pm cst

The problem when adding things to an array, is with java you need to define the array size, and you're not sure how big this is going to be (for example, adding CDs to my java program, http://www.bluetrait.com/projects/dca/).

With PHP the array will just increase if needed. Java doesn't do this.

# Matthom at 1/25/2006 6:03 pm cst

An array would obviously make sense for storing multiple values, but I was thinking more along the lines of initializing variables in JavaScript, for use in manipulating the DOM.

So... with that for loop, not only could you initialize the variables, you could also do some basic initialization tasks, to each variable - provided it was the same for all variables.

# Tim at 1/25/2006 6:15 pm cst

Sounds like a job for variable variables. Don't know if javascript can do it but php can. You make the value of a variable the variable. I don't know what the use of this would be. Sounds like you may be over thinking somethings. I'd be interested in seeing an application of this.

# Matthom at 1/26/2006 6:25 am cst

Tim, yeah, that's kinda what I'm getting at. I looked at that link.. Wow.. gets pretty confusing. All those dollar signs and conversions - I bet it's hard to keep "straight," in the head.

# Jennifer Grucza at 1/26/2006 10:07 am cst

This comment is for Michael:

The problem when adding things to an array, is with java you need to define the array size, and you're not sure how big this is going to be

If you need an array that can grow, why not just use a List? I use Lists ten times as much as I use arrays in Java. The Java Collections framework is enormously useful.

# Aaron M. Clay at 4/1/2008 10:02 am cst

Use something along these lines:

import java.io.; import java.util.; //before the main class public static String variable_name;

//end of before main class FileOutputStream fout; try { fout = new FileOutputStream("variables.var"); int variables = new int(); for (int x =0;x<200;x++) { variablename = variablename +x; variables.add(variable_name);'

} for (int y =0;y

while (in.ready()) { String text = in.readLine(); System.out.println(text); }

catch (Exception ex1) { System.out.println ("Exception ex1 has occurred."); } }

From there, you can just grab the output from the file, or from the output your program just gave. It probably has a few syntax errors, but those are easily fixed.

# Aaron M. Clay at 4/1/2008 10:02 am cst

Oh dear, the formatting was all lost...clean it up, and it should work decently

# Felipe at 1/29/2009 7:09 am cst

for (int i=0; i< Math.pow(3, 2); i++) {

         int ii=0;
         System.out.print(ii);

--- this works for me creates variables: 00, 11 , 22, 33, 44, 55, 66, 77, 88, 99 ALL get initialized to zero.