Multidimensional Array Filtering in VBScript

I am trying to filter a two-dimensional array with VBScript, but the built-in VBScript filter function only works with one-dimensional arrays. I use the array "rs.GetRows ()", just as there is a simple function that works with two-dimensional arrays?

EDIT: It's not about filtering database records, but about filtering multidimensional arrays. I know that I can filter these records at the database level, but this is not what I want. So I'm looking for a filter function for multidimensional arrays.

+4
source share
2 answers
Option explicit ' actual function Public function filter2dArray(a, text) Dim i For i = ubound(a) to lbound(a) step -1 If instr(join(a(i), vbTab), text) = 0 Then ' no match. switch it with ubound and delete ubound a(i) = a(ubound(a)) ReDim preserve a(ubound(a)-1) End If Next filter2dArray = a End Function ' test code Dim b, i b = array( array("row1", "monday", "work"), _ array("row2", "tuesday", "work"), _ array("row3", "wednesday", "free")) b = filter2dArray(b, "work") For i = lbound(b) to ubound(b) msgbox i & ": " & join(b(i), vbTab) Next 

As you requested: filter function for 2D arrays.
Limitations: it only works with text 2d arrays, and it does not have an Include and Compare switch, but it is not difficult.

+2
source

If this is a requirement, you do not change the intermediate data structure, and then iterate over the array and discard elements that do not meet your filter criteria, or adding elements to a new array seems like a good bet. I think the number of records you deal with will be of great importance if you choose this approach.

An alternative would be to load your subset into a temporary table and then a filter. Some code / data indication may help provide a better approach.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1339905/


All Articles