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
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.
Similar Entries
- PHP – passing variables across pages (9706 recent visits)
- Set two variables the same value on one line (71 recent visits)
- The endless loop of organization (2 recent visits)
Stats
601 unique visits since August 2008
Recent Referrers (click)
- variable name java loop
- variable name java loop
- javascript create vars with for loop
- javascript create vars with for loop
- javascript create vars with for loop
- javascript create vars with for loop
- javascript create vars with for loop
- java creating for loop variable
- java creating for loop variable
- php loop to create variables
- fill up an array loop java
- create new variables in a loop
- Define two variable in for loop
- creating variables in a loop as3
- create variables in loop
- javascript create a unique variable from a loop
- php make variables in for loop
- creating variable in php
- create variable loop for mozart
You should be able to do that. I needed to for one of the java things I was doing. ... Read more.