Create variables with a loop? Jan25 '06

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.

Categories: Programming

Add Feedback (view all)

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

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

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 m ... Read more.

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 ex ... Read more.

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

Sounds like a job for variable variables. Don't know if javascript can do it but php ... Read more.

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 ... Read more.

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 ... Read more.

Use something along these lines: import java.io.; import java.util.; //before the main class public static String variable_name; ... Read more.

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

matthom is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Contact Matt

Similar Entries

Stats

601 unique visits since August 2008

Syndicate

Advertisements