2

I want to get color values from a string and assign them like

  n1=red
  n2=blue
  n3=orange
  And so on.... 

Sample code is given below. I don't want to write these, just assign values to n.

   strArray = Split(red, blue, orange, blue)
   Dim i
   For i = 0 to Ubound(strArray)
   response.write strArray(i) & "<br>"
   Next
5
  • "I want to get color values from a string" -- what does the string look like? Also, the use of variables with names like n1,n2,n3 suggests that you should use a single variable which holds an array or dictionary rather than a bunch of separate variables. Commented Jan 25, 2018 at 14:12
  • String is with color as already given strArray = Split(red, blue, orange, blue) I want to use n1, n2, n3 in some other sql query as given below: Commented Jan 25, 2018 at 14:23
  • 1
    But it doesn't make sense to pass 4 arguments to Split. Do you mean Split("red, blue, orange, blue", ",")? Commented Jan 25, 2018 at 14:25
  • This is just example, numbers may vary 4, 5, 6. I want to use then to sql like SELECT * FROM table WHERE column LIKE '%n1%' OR column LIKE '%n2%' OR column LIKE '%n3%' OR column LIKE '%n4%' OR Commented Jan 25, 2018 at 14:27
  • @Babar in which case your whole approach is wrong, dynamically setting variables in VBScript doesn't mean they can then be magically used in a SQL statement. Commented Jan 27, 2018 at 9:12

1 Answer 1

3

You can use Execute:

strArray = Split("red, blue, orange, blue",",")
For i = 0 to Ubound(strArray)
  Execute("n" & (i+1) & " = Trim(strArray(" & i & "))")
Next

Then you will have 4 variables n1,n2,n3,n4 whose values are the successive colors, as the following test shows:

msgbox n1
msgbox n2
msgbox n3
msgbox n4

Warning: never use Execute on a string which an untrusted user has supplied.

Sign up to request clarification or add additional context in comments.

7 Comments

How to do that, i don't see any option here.
There is one problem with sql statement. If there are values of n1,n2,n3,n4 available then sql statement works. But if there are two or three n values, then the LIKE does not find anything. How to match Like values with number of n values in this sql statement.
@Babar the hollowed out tick symbol below the answer votes is how you accept an answer, doing so will turn the tick symbol green. See How does accepting an answer work?.
@Babar there is no mention of a SQL statement in your original question, please don't tack extra queries on to already answered questions. Instead, go and ask another question providing a minimal reproducible example.
@Babar As others have said, you would need to ask a separate question. My knowledge of SQL isn't very deep, but if you ask a question on the SQL tag there would be a better chance of getting a good answer. As far as the mechanics of accepting an answer, there should be a check mark next to the answer to accept it if you want.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.