View Full Version : ASP Array Filter
AnthonyP
08-19-2004, 04:48 PM
Hi everyone,
I have a small problem. I have been trying to match two arrays and get all the non matching values to print out. I tried using the Filter() function, but it either gives one value, even in a FOR NEXT loop. I dunno, I was probably doing it all wrong, but here is what I mean:
<%
listAll = "1, 2, 3, 4, 5, 6, 7"
listBad = "1, 3, 4"
arrAll = split(listAll, ",")
arrBad = split(listBad, ",")
' How do i get the values in arrAll that do not match the values of arrBad?
' Therefore what I need is an array, arrGood contaning 2, 5, 6, 7
%>
Any help will be really appriciated :( I spent hours and hours trying to figure this out!
Thankx,
Anthony.
afterburn
08-19-2004, 04:57 PM
well the filter() function only returns matching values.
I would create my own function that takes a list of items and an array that loops thru using redim preserve keywords to dimension a new array to return....
besides that seems your spliting them both an looking for one instance of it, which is true.
looking in an array with
1,3,5,7
and your passing one item into the filter function looking for it ... its not duplicated....
putts
08-19-2004, 08:36 PM
strList = "1,2,3,4,5,6,7"
strBad = "1,3,6"
arrList = split(strList,",")
arrBad = split(strBad,",")
strResult = ""
for i=0 to ubound(arrList)
blnFound = false
for j=0 to ubound(arrBad)
if arrList(i) = arrBad(j) then
blnFound = true
end if
next
if not(blnFound) then
if strResult = "" then
strResult = arrList(i)
else
strResult = strResult & "," & arrList(i)
end if
end if
next
arrResult = split(strResult,",")
As usual, this is just made up and, thus, not tested.
Give it some runs and see if it works for you or not.
AnthonyP
08-22-2004, 08:26 AM
Originally posted by afterburn
well the filter() function only returns matching values.
Actually, I think you can get non matchign values from filter by using "false at the end:
Filter(array, String, False) ... for non matchign values
Filter(array, String, True) ... True is default, (matchign values)
AnthonyP
08-22-2004, 08:45 AM
Originally posted by putts
As usual, this is just made up and, thus, not tested.
Give it some runs and see if it works for you or not. [/B]
Thanks putts! This one seems similar to the one I got. My internet was down, so I was tryign to figure this out on my own the last two days since I can only come online from my friends house... this is how I did it:
<%
listA = "a, b, c, d, e, f, g"
listB = "a, c, g"
arrA = split(listA, ",")
arrB = split(listB, ",")
For i=0 to ubound(arrB)
For j=0 to ubound(arrA)
If arrA(j) = arrB(i) then
arrA(j) = Null
End if
next
next
For k=0 to ubound(arrA)
Response.write arrA(k)
next
%>
that too me a while to figure out... was very frustrating! But now that I have plugged it into my DB, and the actual script I want it to work with, i am gettin gall sorts of errors. I was wondering if you guys can help me out! Im really stuck! lol..
This is the error:
Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'ubound'
/s/xtra/goals/~view.goals2.asp, line 218
<%
' ***** BEGIN: CONNECTION *****
'****************************************************************************************
Dim adoCon
Dim rs
Dim strSQL
Dim arrTable
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open xtradb
strSQL = "SELECT * FROM goals ORDER BY deadline"
set rs = adoCon.execute(strSql)
'****************************************************************************************
' ***** END: CONNECTION *****
' ***** BEGIN: GOALS LIST *****
if not(rs.eof) then
arrTable = rs.getRows
for i=0 to ubound(arrTable,2)
If arrTable(1,i) = who then
If arrTable(11,i) = "yes" then
Response.write "Goal Completed<br>"
Else
%>
<!-- ****************************************************************************************** -->
<div align="center">
<center>
<table border="1" cellpadding="2" style="border-collapse: collapse" bordercolor="#111111" width="500" height="1%" bgcolor="FFFFFF">
<form action="~view.goals2.asp" method="post" name="updategoal" id="updategoal">
<tr>
<td width="36" height="0%" valign="top" align="right" bordercolor="#FFFFFF">
<p align="right"> </td>
<td width="464" height="0%" valign="top" bordercolor="#FFFFFF">
<font size="2">
<%
If arrTable(8,i) = "short" then
Response.write "Short Term Goal"
elseif arrTable(8,i) = "long" then
Response.write "Long Term Goal"
Else
Response.write "Error!"
End if
%> -
<%
If arrTable(4,i) = 1 then
Response.write "Priority is <strong>Not Important</strong>"
Elseif arrTable(4,i) = 2 then
Response.write "Priority is <strong>Less Important</strong>"
Elseif arrTable(4,i) = 3 then
Response.write "Priority is <strong>Normal</strong>"
Elseif arrTable(4,i) = 4 then
Response.write "Peiority is <strong>Important</strong>"
Elseif arrTable(4,i) = 5 then
Response.write "Priority is <strong><font color=""red"">Very Important</font></strong>"
Else
Response.write "Error!"
End if
%>
</font>
</td>
</tr>
<tr>
<td width="36" height="0%" valign="top" align="right" bordercolor="#FFFFFF">
<input type="checkbox" name="end" value="yes"></td>
<td width="464" height="0%" valign="top" bordercolor="#FFFFFF">
<b><font size="2" color="green">
<%= arrTable(3,i) %>
</font></b>
</td>
</tr>
<tr>
<td width="36" height="0%" valign="top" align="right" bordercolor="#FFFFFF"> </td>
<td width="464" height="0%" valign="top" bordercolor="#FFFFFF">
<font size="2"><em><font color="#800000">
<%= arrTable(6,i) %>
</font></em></font>
</td>
</tr>
<%
If NOT isNull(arrTable(7,i)) then
arrA = split(arrTable(7,i), ",", -1, 1)
Else
arrA = Null
End if
If NOT isNull(arrTable(12,i)) then
arrB = split(arrTable(12,i), ",", -1, 1)
Else
arrB = Null
End if
If NOT isNull(arrB) then
For j=0 to ubound(arrB)
If NOT isNull(arrA) then
For k=0 to ubound(arrA)
If arrA(k) = arrB(j) then
arrA(k) = Null
End if
next
End if
next
End if
If NOT isNull(arrA) then
for sg=0 to ubound(arrA)
%>
<tr>
<td width="36" height="0%" valign="top" align="right" bordercolor="#FFFFFF">
</td>
<td width="464" height="0%" valign="top" bordercolor="#FFFFFF">
<font size="2">
<%
If NOT isNull(arrA(sg)) then
Response.write "<input type=""checkbox"" name=""subgoals"" value=""" & arrA(sg) & """>"
Response.write arrA(sg)
End if
%>
</font>
</td>
</tr>
<%
next
End if
%>
<tr>
<td width="36" height="0%" valign="top" align="right" bordercolor="#FFFFFF"> </td>
<td width="464" height="0%" valign="top" bordercolor="#FFFFFF">
<b><br><font size="2">Deadline: [<font color="#FF0000"><%= MonthName(Month(arrTable(5,i))) & " " & Day(arrTable(5,i)) & ", " & Year(arrTable(5,i)) %></font>] </font> </b>
<font size="2">- Since: [<font color="#0000FF"><%= MonthName(Month(arrTable(9,i))) & " " & Day(arrTable(9,i)) & ", " & Year(arrTable(9,i)) %></font>]</font></td>
</tr>
<tr>
<td width="36" height="0%" valign="top" align="right" bordercolor="#FFFFFF"> </td>
<td width="464" height="0%" valign="top" bordercolor="#FFFFFF">
<font size="2">Status: <input type="text" name="status" class="lean" size="75" value="<%= arrTable(10,i) %>"></font>
</td>
</tr>
<tr>
<td width="500" height="1%" valign="top" align="right" colspan="2" bordercolor="#FFFFFF">
<p align="center">
<input type="submit" name="submit" value="save" class="lean">
<input type="submit" name="submit" value="delete" class="lean">
<br><br>
</td>
</tr>
<input type="hidden" name="id" value="<%= arrTable(0,i) %>">
</form>
</table>
</center>
</div>
<br>
<!-- ****************************************************************************************** -->
<%
If NOT isNull(arrTable(12,i)) then
arrC = split(arrTable(12,i), ",", -1, 1)
Response.write arrC
response.end
End if
End if
End if
Next
End if
' ***** END: GOALS LIST *****
' ***** BEGIN: SAVE GOAL EDITS *****
If Request("submit") = "save" then
If NOT isNULL(Request("id")) or Request("id") = "" then
If NOT isNull(arrC) then
For sd=0 to ubound(arrC)
ListB = ListB & arrC(sd) & ","
Next
Else
ListB = Null
End if
listB = Replace(Request("subgoals"), ", ", ", ") & ", " & ListB
ListB = Replace(ListB, ",,", ",")
ListB = Replace(ListB, " ", " ")
Dim adoConb
Dim strSQLb
Dim rsAddDatab
Set adoConb = Server.CreateObject("ADODB.Connection")
adoConb.Open xtradb
Set rsAddDatab = Server.CreateObject("ADODB.Recordset")
strSQLb = "SELECT * FROM goals WHERE id=" & Request("id")
rsAddDatab.CursorType = 2
rsAddDatab.LockType = 3
rsAddDatab.Open strSQLb, adoConb
rsAddDatab.Fields("end") = Request("end")
rsAddDatab.Fields("status") = Request("status")
rsAddDatab.Fields("subdone") = ListB
rsAddDatab.Update
rsAddDatab.Close
Set adoConb = Nothing
Set strSQLb = Nothing
Response.redirect "~view.goals2.asp"
End if
End if
' ***** END: SAVE GOAL EDITS *****
%>
and this is line 218 and following...
If NOT isNull(arrC) then
For sd=0 to ubound(arrC)
ListB = ListB & arrC(sd) & ","
Next
Else
this error is happening ONLY when
arrC = split(arrTable(12,i), ",", -1, 1)
when there is no data in arrTable(12,i).
If that field in the DB is blank, then I get thsi error. I tried other ways of fixing it, but every time I try to fix it, i get an error.
I hope you can help me! I woudl really apprciate it, cos im really stuck on this one!
AnthonyP
08-22-2004, 08:47 AM
P.S. .. soprry abotut the messy code! ... im a very sloppy coder :crying:
EDIT: one more thing, ... the error occours when the form that is generated in the script is submitted while the values in arrTable(12,i) are blank!
afterburn
08-23-2004, 12:53 PM
upload the file. Paste the complete error message details in the forum also.
AnthonyP
08-24-2004, 09:51 PM
thankx afterburn :) .. here is the complete file attached! It wont let me upload ASP files, so I put it in a text file for you to rename. Also, this is the exact error message:
Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch: 'ubound'
/s/xtra/goals/~view.goals2.asp, line 220
Browser Type:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)
Page:
POST 42 bytes to /s/xtra/goals/~view.goals2.asp
POST Data:
subgoals=a&status=Pending&submit=save&id=1
It happens when I press the submit button on the form. Of course, when I try to fix thsi by whats cauins the problem, I get other errors :( ...
I hope you can help! I would really appriciate it!
AnthonyP
08-24-2004, 10:04 PM
and here is the DB file exactly as it appears when the error occurs! I had export the table to an HTML file since the forum does nto allow .mdb file uploads. goals is the table, and the DB is xxtra.mdb .. Also, I am allowed only one upload at a time, so I had to make this second post!
The error happens onle if the "subdone" cell being called is empty. I tried "IF NOT isNull" to go around this, and I tried everything I could think of but I still get more errors or it just wont work. :( ... When I fix the problem for no errors to happen, then the form does nto save properly!
What the form is supposed to do is allow the user to remove the sub goals by checking the corresponding check box and clicking save.
afterburn
08-25-2004, 06:48 AM
the error occurs because the value is not an array.
instead of an isNull statement you need an IsArray ....
AnthonyP
08-25-2004, 07:19 AM
ooh! Now I understand! I didnt know an isArray function even existed! Thanks a lot afterburn! I really appriciate it!
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.