从Vbscript中的2D数组中删除重复值

时间:2014-12-18 10:08:51

标签: arrays vbscript

我有一个2D数组

原始2D数组

ArrStore(0,0)="Volvo"
ArrStore(0,1)="BMW"
ArrStore(0,2)="Ford"
ArrStore(0,3)="Ford"
ArrStore(0,4)="Ford"
ArrStore(1,0)="Apple"
ArrStore(1,1)="Orange"
ArrStore(1,2)="Banana"
ArrStore(1,3)="Orange"

现在我想从中删除Deuplicate值。

预期输出必须在2D数组中(见下文)

ArrStore(0,0)="Volvo"
ArrStore(0,1)="BMW"
ArrStore(0,2)="Ford"
ArrStore(1,0)="Apple"
ArrStore(1,1)="Orange"
ArrStore(1,2)="Banana"

如果我正在使用" Dictionary"然后在Stackoverflow上搜索其删除2D数组pattern.i,但所有问题都与一维数组有关,不适用于二维数组

2 个答案:

答案 0 :(得分:1)

假设你

  1. 坚持使用rectangular array
  2. 可以使用空单元格
  3. 表示"每行/每列唯一"
  4. 您可以使用字典并将唯一元素移动到左侧/顶部:

      Dim a(1, 4)
      a(0,0)="Volvo"
      a(0,1)="BMW"
      a(0,2)="Ford"
      a(0,3)="Ford"
      a(0,4)="Ford"
      a(1,0)="Apple"
      a(1,1)="Orange"
      a(1,2)="Banana"
      a(1,3)="Orange"
      WScript.Echo "Before:"
      disp2DA a
      uniq2DA a
      WScript.Echo "After:"
      disp2DA a
    
    Sub uniq2DA(a)
      Dim d : Set d = CreateObject("Scripting.Dictionary")
      Dim i, j, k
      For i = 0 To UBound(a, 1)
          d.RemoveAll
          k = 0
          For j = 0 To UBound(a, 2)
              If Not d.Exists(a(i, j)) Then
                 d(a(i, j)) = Empty
                 a(i, k) = a(i, j)
                 k = k + 1
              End If
          Next
          For j = k To UBound(a, 2)
              a(i, j) = Empty
          Next
      Next
    End Sub
    
    Sub disp2DA(a)
      Dim i, j, s
      For i = 0 To UBound(a, 1)
          For j = 0 To UBound(a, 2)
              If IsEmpty(a(i,j)) Then s = " <Empty>" Else s = " " & a(i, j)
              WScript.StdOut.Write s
          Next
          WScript.Echo
      Next
    End Sub
    

    输出:

    Before:
     Volvo BMW Ford Ford Ford
     Apple Orange Banana Orange <Empty>
    After:
     Volvo BMW Ford <Empty> <Empty>
     Apple Orange Banana <Empty> <Empty>
    

    如果您愿意使用参差不齐的数组,可以使用here中的uniqFE()函数:

      Dim a : a = Array( _
           Split("Volvo BMW Ford Ford Ford") _
         , Split("Apple Orange Banana Orange Pear") _
      )
      WScript.Echo "Before:"
      disp2DAoA a
      Dim i
      For i = 0 To UBound(a)
          a(i) = uniqFE(a(i))
      Next
      WScript.Echo "After:"
      disp2DAoA a
    
    Sub disp2DAoA(a)
      Dim e
      For Each e In a
          WScript.Echo Join(e)
      Next
    End Sub
    
    ' returns an array of the unique items in for-each-able collection fex
    Function uniqFE(fex)
      Dim dicTemp : Set dicTemp = CreateObject("Scripting.Dictionary")
      Dim xItem
      For Each xItem In fex
          dicTemp(xItem) = 0
      Next
      uniqFE = dicTemp.Keys()
    End Function
    

    输出:

    Before:
    Volvo BMW Ford Ford Ford
    Apple Orange Banana Orange Pear
    After:
    Volvo BMW Ford
    Apple Orange Banana Pear
    

答案 1 :(得分:0)

下面的代码可能对你有帮助, 我已将此用于 2D

One dim的参考链接。代码是&gt; http://blogs.technet.com/b/heyscriptingguy/archive/2006/10/27/how-can-i-delete-duplicate-items-from-an-array.aspx

ArrStore(0,0)="Volvo"
ArrStore(0,1)="BMW"
ArrStore(0,2)="Ford"
ArrStore(0,3)="Ford"
ArrStore(0,4)="Ford"
ArrStore(1,0)="Apple"
ArrStore(1,1)="Orange"
ArrStore(1,2)="Banana"
ArrStore(1,3)="Orange"

for i=0 to 2
    for j=0 to 2
        If Not objDictionary.Exists(ArrStore(i,j)) Then
            objDictionary.Add ArrStore(i,j), ArrStore(i,j)   
        End If
    next
next



intItems = objDictionary.Count - 1

ReDim ArrStore(intItems, intItems)

k = 0

For Each strKey in objDictionary.Keys
    for i=0 to 2
        for j=0 to 2
            ArrStore(i,j) = strKey
            k = k + 1
        next
    next
Next

for i=0 to 2
    for j=0 to 2
        Wscript.Echo ArrStore(i,j)
    next
next