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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if in a List

Status
Not open for further replies.

Billybong007

IS-IT--Management
Mar 21, 2007
10
GB
Howde,

If there an easier way of creating an IF statement to check if a variable is in a list rather than doing an ELSE statment.

I have a list of numbers e.g. 22, 44, 3, 54 etc
I need to know if a variable "ID" is a number inside these numbers.

Since the list of numbers will change frequently it would become difficult to update a number of IF statements.

Sorry for such a novice question.

Tnks for your help

 
The code below is a starting point. You can experiment with it to get something that will be much better than a string of IF statments.
Code:
<%
bFound = False
strFind = "3"
strList = "22, 44, 3, 54"

MyArray = Split(strList, ", ")
For i = 0 to uBound(MyArray)
  If MyArray(i) = strFind Then
    bFound = True
    Exit For
  End If
Next

If bFound Then
  Response.Write "The value was found."
Else
  Response.Write "The value was NOT found."
End If
%>
 
You could also check for it this way...

Code:
<%

Dim List

List = "22, 44, 3, 54"
strFind = "3"

List = Replace(List, " ", "")

If Instr(1, "," & List & ",", "," & strFind & ",") Then
	Response.Write("In List")
Else
	Response.Write("Not In List")
End If

%>

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks George also,

I discovered that I would be better to write this is Javascript as I would need a "onclick" check rather than a page submission check (i basically want a field to appear when a radio button of a certain value is selected)

I don't know if you are any good at Javascript as well so I will also post this in the Javascript section.

I have used your above code and hacked away at is using other tutorials on the net and came up with :

Code:
<script>
function whichButton() {
var group1Checked 
var myString = "11,12,3,22,18,19,55";
var mySplitResult = myString.split(",");

for(i = 0; i < mySplitResult.length; i++){
	document.write("<br /> Element " + i + " = " + mySplitResult[i]); 
}

	for (var i=0; i<document.survey.optSecenek.length; i++) {
	
		if (document.survey.optSecenek[i].checked) {
		group1Checked = document.survey.optSecenek[i].value
		}
		}
			
	if(!group1Checked) == (!mySplitResult){ //if group1Checked does not equal null
	alert("Found You")
	}
	else{
	alert("Not found")
	} 

}


</script>

I can get the scripts working seperately i.e. I have learnt now how to split the variable and display the results.

I have also been able to determine the value of a dynamic number of radio buttons and display the value - so I added the two scripts together with a few edits to determine if (!group1Checked) == (!mySplitResult)but it fails with a warning of "Object required"

Any ideas what it's looking for ?
 
Hmmm.. your missing a couple semi-colons:
Code:
<script>
function whichButton() {
var group1Checked[COLOR=red];[/color]
var myString = "11,12,3,22,18,19,55";
var mySplitResult = myString.split(",");

for(i = 0; i < mySplitResult.length; i++){
    document.write("<br /> Element " + i + " = " + mySplitResult[i]);
}

    for (var i=0; i<document.survey.optSecenek.length; i++) {
    
        if (document.survey.optSecenek[i].checked) {
        group1Checked = document.survey.optSecenek[i].value[COLOR=red];[/color]
        }
        }
            
    if(!group1Checked) == (!mySplitResult){ //if group1Checked does not equal null
    alert("Found You")[COLOR=red];[/color]
    }
    else{
    alert("Not found")[COLOR=red];[/color]
    }

}


</script>

Also your comparing the value from the last checked radio to an array of values, which will never work (well, almost never).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top