
using Array element to define variables
Here's what I'd like to wind up with:<BR>Dim var1, var2, var3<BR>var1=1<BR>var2=2<BR>var3=3<BR><BR>Can I accomplish the same thing using array elements. Something like (this code didn't work):<BR>Dim varArray(2), valueArray(2)<BR>varArray1(0) = "var1"<BR>varArray2(1) = "var2"<BR>varArray3(2) = "var3"<BR><BR>valueArray(0) = 1<BR>valueArray(1) = 2<BR>valueArray(2) = 3<BR><BR>Dim var1, var2, var3<BR>response.write(varArray1(0)) = valueArray(0)<BR>response.write(varArray1(1)) = valueArray(1)<BR>response.write(varArray1(2)) = valueArray(2)<BR><BR>I want the code to execute the same as:<BR>var1=1<BR>var2=2<BR>var3=3<BR><BR>Is this possible?<BR><BR>Thanks in advance,<BR>Chris<BR>cjg_64@yahoo.com

sounds like you want...
... an associative array. look up Scripting.Dictionary<BR><BR>j<BR>http://rtfm.atrax.co.uk/

I have to ask...
...what is wrong with simply doing:<BR><BR>var(1) = 1<BR>var(2) = 2<BR>var(3) = 3<BR><BR>And then every place where you were going to use <BR> Response.Write var1<BR>you write<BR> Response.Write var(1)<BR>instead?????<BR><BR><BR>

RE: I have to ask...
It's really hard to discribe without type a lot of detail...but basically, I am randomizing the order of 2 arrays. I would like one array to contain variable names and set these variables equal to the values specified in another array. I can accomplish this with a bunch of "IF" statements but thought this would be a neat way of doing it if it actually could work.

I don't understand...
...why you care about the "variable names". What's the difference between using <BR> var(1)<BR>and <BR> var1<BR>in your coding???<BR><BR>Or, for that matter,<BR> var( pseudoNameArray(1) )<BR>???<BR><BR>You'll have to work hard to convince me that you need "a bunch of IF statements" to accomplish this. Oh, probably you do using the code you are thinking of. But now try tossing out that code and using a completely different approach.<BR><BR>Show some real code, if you want to really convince us.<BR><BR>By the way, there *IS* a way to do this. But it gives *HORRIBLE* performance. Probably slow down your pages by an order of magnitude! But if you don't care about performanceand you don't want to try thinking "outside the box"then feel free to look into VBScript's Execute statement. It *is* in the docs.<BR><BR>

additional...
.... execute() can be a major security risk if userinput is involved  it could give an attacker the chance to run arbirtrary code against your server. try not to use it if at all possible, quite aside from the performance degradation.<BR><BR><BR>j<BR>http://rtfm.atrax.co.uk/

RE: I don't understand...
OK...here's the code I am using that works:<BR><BR><%<BR>'***CREATE ARRAY FOR "NEXT PAGE" NUMBERS <BR>'(identifies the next page to show).<BR>Dim fp_Array(2) 'elements 0,1,2<BR>fp_Array(0) = 2<BR>fp_Array(1) = 5<BR>fp_Array(2) = 8<BR><BR>Dim np_Array(2) 'elements 0,1,2<BR>Dim np_a, np_b, np_c, np_d 'pass these vars to specify the next page<BR>np_Array(0) = "b"<BR>np_Array(1) = "c"<BR>np_Array(2) = "d"<BR><BR>'*MyArray4 is difined earlier and contains elements that have been sorted.<BR>np_a = fp_Array(MyArray4(0))<BR><BR>IF np_Array(MyArray4(0)) = "b" then<BR> np_b = fp_Array(MyArray4(1))<BR>Elseif np_Array(MyArray4(0)) = "c" then<BR> np_c = fp_Array(MyArray4(1))<BR>Elseif np_Array(MyArray4(0)) = "d" then<BR> np_d = fp_Array(MyArray4(1))<BR>END IF<BR><BR>IF np_Array(MyArray4(1)) = "b" then<BR> np_b = fp_Array(MyArray4(2))<BR>Elseif np_Array(MyArray4(1)) = "c" then<BR> np_c = fp_Array(MyArray4(2))<BR>Elseif np_Array(MyArray4(1)) = "d" then<BR> np_d = fp_Array(MyArray4(2))<BR>END IF<BR><BR>IF np_Array(MyArray4(2)) = "b" then<BR> np_b = 11<BR>Elseif np_Array(MyArray4(2)) = "c" then<BR> np_c = 11<BR>Elseif np_Array(MyArray4(2)) = "d" then<BR> np_d = 11<BR>END IF<BR>%><BR>'*END CODE*<BR><BR>I would like to replace the "IF" statements with the array items as variables.

Try this...
<%<BR>'***CREATE ARRAY FOR "NEXT PAGE" NUMBERS <BR>'(identifies the next page to show).<BR>Dim fp_Array<BR>fp_Array = Array(2,5,8)<BR><BR>Dim np_Array<BR>np_Array = Array("b", "c", "d")<BR><BR>Dim np_abcd(3)<BR>CONST NPA = 1<BR>CONST NPB = 2<BR>CONST NPC = 3<BR>CONST NPD = 4<BR><BR>'*MyArray4 is difined earlier and contains elements that have been sorted.<BR>np_abcd(NPA) = fp_Array(MyArray4(0))<BR><BR>np_abcd( ASC(np_Array(MyArray4(0))) AND 7 ) = fp_Array(MyArray4(1))<BR><BR>np_abcd( ASC(np_Array(MyArray4(1))) AND 7 ) = fp_Array(MyArray4(2))<BR><BR>np_abcd( ASC(np_Array(MyArray4(0))) AND 7 ) = 11<BR>%><BR><BR>And now instead of using np_a, np_b, et al., you use<BR> np_abcd(NPA)<BR><BR>See? Outside the box.<BR><BR>But I grant you that it's kind of ugly and convoluted.<BR><BR>Let me think on it and see if there's yet another 3 or 4 ways.<BR><BR>

RE: Try this...
you're right about the "ugly and convoluted" thing...it's a little easier to follow the "IF" statements. What's the "AND 7" code for? I was really only looking for a way to use an element from an array as a variable name, and then to assign a value to that variable...thought it was worth finding out about.

Well, you could use...
...a Scripting.Dictionary object.<BR><BR>So instead of doing<BR> np_a = xxx<BR>you'd do<BR> myDictionary("np_a") = xxx<BR>and instead of doing<BR> Response.Write np_a<BR>you'd do<BR> Response.Write myDictionary("np_a")<BR><BR><shrug>There aren't lots of pretty ways to do this.</shrug><BR><BR>***********<BR>The AND operator in VBScript works as a BITWISE AND when you use it between two integers.<BR><BR>So doing <BR> ASC(xxx) AND 7<BR>means you are taking the ASCII value of the character given by xxx and then "masking" it to only 3 bits.<BR><BR>So: <BR> ASC("a") AND 7 >> 97 AND 7 >> 0x61 AND 0x07 > 1<BR> ASC("b") AND 7 >> 98 AND 7 >> 0x62 AND 0x07 > 2<BR> ASC("c") AND 7 >> 99 AND 7 >> 0x63 AND 0x07 > 3<BR><BR>To use all the letters "a" to "c" you would just use<BR> AND 31<BR>and it would give you values from 1 to 26.<BR><BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

