Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create and use dynamic varialbes and arrays.

Status
Not open for further replies.

solepixel

Programmer
May 30, 2007
111
US
I'm having the hardest time getting this to work. I'm trying to generate a lot of stuff dynamically to allow for my script to be easily customizable and I can't figure out a way around this.

I have to store some objects in an array and i have several different arrays I need to keep them in. I tried eval, but that doesn't seem to be working. then I tried creating a new object, but what I figured out is I need a separate object for each dynamic variable, which brings me back to square one.

This is the javascript I need
Code:
var the_numbers = new Array();
var the_digits = new Array();
var the_emails = new Array();

And this is what I'm trying to use to get it
Code:
for(items in custom_variables){
	eval("var " + custom_variables[items]['indexName'] + " = new Array()");
}

What is the best way to do this?
 
Why not use the technique I gave you in your previous post?

thread216-1389972

Something like:

Code:
var custom_variables = { the_numbers: new Array(), the_digits: new Array(), the_emails: new Array() };

or even:

Code:
var custom_variables = { the_numbers: [], the_digits: [], the_emails: [] };

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I was trying to adapt it, but couldn't figure it out. After playing with it for several hours last night, and "guessing" at ways to do it, I actually ended up creating my variable like this:

Code:
window['custom_variables'] = {
    the_numbers : {
        item1: 'stuff',
        item2: 'stuff',
        item3: 'stuff'
    },
    the_emails : {
        item1: 'stuff',
        item2: 'stuff',
        item3: 'stuff'
    }
}

Referencing custom_variables[item]['item1'] (like you mentioned in the previous post) posed problems when trying to use..... AHH, ok, I just figured it out.. This isn't an actual arrray!!! It just occured to me that I wasn't able to loop with the traditional for(i=0 loop and custom_variables.length never returned a value! That's why you told me to do for-in loops.... I feel dumb now! So I need to do for-in loops inside the main for-in loop to get the 3 values out of the 2nd level! DUH!

I may go back and try it again, by adding "new array()" to it, but this way seems to be working ok. Would there be any negative outcome to sticking with the way it is now? (ie browser incompatibility?)
 
Shouldn't be - the only possible weirdness i can think of is if you happen to use the "Prototype" JS framework which extends some object types... but you're probably not, so should be OK.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Actually, I am using prototype. Why do you assume I'm not? Is there something in Prototype that can help me out? And since I am, would you recommend I go back and try your method?
 
Actually, I am using prototype. Why do you assume I'm not?

Erm... don't take this the wrong way, but IMHO, most people who use the Prototype framework would already know about creating objects / associative arrays.

I guess I'm wrong on that one!

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Well, I went ahead and reverted to the way you told me and it is all working except for the RegExp part. It's being a real PITA. I either get an error setting the variable to new RegExp(myVariableStringRegExp) or it never matches, even when given a good value.
 
when I try:
Code:
var custom_variables = {
    usphone: {
        className: "jsvalidate_usphone",
        defaultMessage: "This field must contain a valid US phone number.",
        regExp: new RegExp("^(\(?[0-9]{3}[\)-\. ]?\ ?)?[0-9]{3}[-\. ]?[0-9]{4}$")
    }
it breaks at the line the regExp is created with "invalid quantifier
 
I would prefer to store the values like this:
Code:
var custom_variables = {
    usphone: {
        className: "jsvalidate_usphone",
        defaultMessage: "This field must contain a valid US phone number.",
        regExp: "^(\(?[0-9]{3}[\)-\. ]?\ ?)?[0-9]{3}[-\. ]?[0-9]{4}$"
    }
}
 
The "invalid quantifier" is nothing to do with the way you're storing the regexp - it's to do with the regexp itself. Try assigikng it to a new variable on a new line by itself - I see the same error.

If you can't store it "normally", anything else you try will fail.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
BillyRayPreachersSon said:
Try assigning it to a new variable on a new line by itself
I'm not quite sure what you mean here? You're talking about inside my object? Or when I'm trying to test my value?
 
I figured out that if I store it like this:

Code:
usphone: {
    className: "usphone",
    defaultMessage: "This field must contain a valid US phone number.",
    regExp: /^(\(?[0-9]{3}[\)-\. ]?\ ?)?[0-9]{3}[-\. ]?[0-9]{4}$/
},

Then it works. I guess i just have to remember the slashes in front and back. Thanks for all the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top