I have a console utility I am trying to build and I am wondering what's
the best way to go about checking one array to see if each of it's
values are in another array
Let me explain.
I have a text file which is set up like this:
UserID1,Group1 Group2 Group3 Group4
UserID2,Group1 Group3 Group4
UserID2,Group5 Group6
So, one UserID can be on multiple lines, but groups never repeat.
I also have setup an array of groups that the user SHOULD be a member of.
GroupsNeeded = ("Group1", "Group2", "Group3", "Group4")
(I don't know how to initialize
multiple values in an array, so I know this isn't right, I did it line
by line)
But that's not the problem...
The problem is I need to know how I can loop through each user and each
group the user is a member of, and test to see that each group THE USER
IS A MEMBER OF is contained in the GroupsNeeded array. I know it
sounds backwards, but that's what I have to do.
Here is what I've come up with so far, but there is something wrong with my logic as my output is a off by quite a bit.
PS: I know my code is ugly, I'm just trying to get through this as quickly as possible.

Check Array for in another Array
dehran ph
hi,
blair sorry forgive my ignorant i saw something in your code many times but i didn't get what is it would you plz tell me what is it exactly
Public Class SetCollection(Of T)
Implements IEnumerable(Of T)
Private m_list As New List(Of T)
what is this (Of T)
i have tried this and it worked with me
Sub Main()
Dim UserGroups As String() = {"group1", "group2", "group3"} For Each singlegroup As String In UserGroups If compare2strings(singlegroup) = True ThenConsole.WriteLine(
"successfull group : {0}", singlegroup) ElseConsole.WriteLine(
"group not exist : {0}", singlegroup) End If Next
End Sub Public Function compare2strings(ByVal group As String) Dim groups As String() = {"group1", "group3"} Dim bool As Boolean = False For Each singlegroup As String In groups If String.Compare(group, singlegroup) = 0 Thenbool =
True End If Next Return bool End Functionwhen you intialize array you simply use = {"a", "s", "d","f") if its multi D. array like for example groups(3,) you intialize it ={("a","s"),("d","f"),("g","h")}
hope this will help
best regards
Steven Hemingray - MSFT
ermmm. . . thanks. . . its actually a port of code from this book -
C# Cookbook (Cookbooks (O'Reilly))
jasMSDN
I Gotta a Give it to You jmcihinney...... You are Good.... I Apologize for the Ruckus We got into.... Cheers
Joel_F
one thing missing from .NET collections is "Sets"
you may find this class useful (translated from c#) Let me know oif it doesn't make sense -
(Note: at the risk of starting a holy war, this class ABSOLUTELY demonstrates the inferiority of the VB language.
Just to name a few )
Imports System.Collections.Generic
Imports System.Text
Public Class SetCollection(Of T)
Implements IEnumerable(Of T)
Private m_list As New List(Of T)
Public ReadOnly Property Count() As Integer
Get
Return m_list.Count
End Get
End Property
Public Property Item(ByVal index As Integer) As T
Get
Return m_list(index)
End Get
Set(ByVal value As T)
If Me.Contains(value) Then _
Throw New ArgumentException("Duplicate object.")
m_list(index) = value
End Set
End Property
Public Sub Add(ByVal item As T)
If Me.Contains(item) Then _
Throw New ArgumentException("Duplicate object.")
m_list.Add(item)
End Sub
Public Sub Remove(ByVal item As T)
If Not Me.Contains(item) Then _
Throw New ArgumentException("Item not in collection.")
m_list.Remove(item)
End Sub
Public Sub RemoveAt(ByVal index As Integer)
m_list.RemoveAt(index)
End Sub
Public Function Contains(ByVal item As T)
Return m_list.Contains(item)
End Function
Public Shared Operator And(ByVal lhs As SetCollection(Of T), _
ByVal rhs As SetCollection(Of T)) _
As SetCollection(Of T)
Return lhs.UnionOf(rhs)
End Operator
Public Function UnionOf(ByVal aSet As SetCollection(Of T))
Dim unionSet As New SetCollection(Of T)
Dim sourceSet As SetCollection(Of T) = Nothing
Dim mergeSet As SetCollection(Of T) = Nothing
If aSet.Count > Me.Count Then
sourceSet = aSet
mergeSet = Me
Else
sourceSet = Me
mergeSet = aSet
End If
For Each item As T In sourceSet
unionSet.Add(item)
Next
For Each item As T In mergeSet
If Not unionSet.Contains(item) Then unionSet.Add(item)
Next item
Return unionSet
End Function
Public Shared Operator Or(ByVal lhs As SetCollection(Of T), _
ByVal rhs As SetCollection(Of T)) _
As SetCollection(Of T)
Return lhs.Intersection(rhs)
End Operator
Public Function Intersection(ByVal aSet As SetCollection(Of T)) _
As SetCollection(Of T)
Dim commonSet As New SetCollection(Of T)
Dim sourceSet As SetCollection(Of T) = Nothing
Dim mergeSet As SetCollection(Of T) = Nothing
If aSet.Count > Me.Count Then
sourceSet = aSet
mergeSet = Me
Else
sourceSet = Me
mergeSet = aSet
End If
For Each item As T In mergeSet
If sourceSet.Contains(item) Then commonSet.Add(item)
Next
Return commonSet
End Function
Public Shared Operator Xor(ByVal lhs As SetCollection(Of T), _
ByVal rhs As SetCollection(Of T)) _
As SetCollection(Of T)
Return lhs.Difference(rhs)
End Operator
Public Function Difference(ByVal aSet As SetCollection(Of T)) _
As SetCollection(Of T)
Dim differSet As New SetCollection(Of T)
For Each item As T In aSet
If Not Me.Contains(item) Then differSet.Add(item)
Next
For Each item As T In Me
If Not aSet.Contains(item) Then differSet.Add(item)
Next
Return differSet
End Function
Public Shared Operator =(ByVal lhs As SetCollection(Of T), _
ByVal rhs As SetCollection(Of T)) As Boolean
Return lhs.Equals(rhs)
End Operator
Public Shared Operator <>(ByVal lhs As SetCollection(Of T), _
ByVal rhs As SetCollection(Of T)) As Boolean
Return Not lhs.Equals(rhs)
End Operator
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If obj Is Nothing Then Return False
If Not TypeOf obj Is SetCollection(Of T) Then Return False
If Me.Count <> CType(obj, SetCollection(Of T)).Count Then Return False
If Not Me.IsSubsetOf(CType(obj, SetCollection(Of T))) Then Return False
If Not CType(obj, SetCollection(Of T)).IsSubsetOf(Me) Then Return False
Return True
End Function
Public Overrides Function GetHashCode() As Integer
Return m_list.GetHashCode()
End Function
Public Function IsSubsetOf(ByVal aSet As SetCollection(Of T)) As Boolean
For Each item As T In Me
If Not aSet.Contains(item) Then Return False
Next
Return True
End Function
Public Function IsSupersetOf(ByVal aSet As SetCollection(Of T)) As Boolean
For Each item As T In aSet
If Not Me.Contains(item) Then Return False
Next
Return True
End Function
Public Function DisplaySet() As String
If Me.Count = 0 Then Return "{}"
Dim display As New StringBuilder("{ ")
For i As Integer = 0 To Me.Count - 2
display.Append(Me.Item(i))
display.Append(", ")
Next
display.Append(Me.Item(Me.Count - 1))
display.Append(" }")
Return display.ToString()
End Function
Public Function GetEnumerator1() As System.Collections.Generic.IEnumerator(Of T) _
Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
Return m_list.GetEnumerator()
End Function
Public Function GetEnumerator2() As System.Collections.IEnumerator _
Implements System.Collections.IEnumerable.GetEnumerator
Return m_list.GetEnumerator()
End Function
End Class
mjcs100
first of all. . . there is a typo in my code. . .
the DisplaySetFunction Should have read:
dr.thomas
ManYou Are Political..... Why Don't you Change the Vinyl and Put some Mick Jagger or the Beatles On..... "Hell No We Won't Go.... "Hell No We Won't Go"................. Cheers!!!!!
buladbanaw
or using the above SetCollectionFromArray class
MessageBox.Show( _
iif(New SetCollectionFromArray(Array1).IsSubsetOf(new SetCollectionFromArray(Array2))),
"All elements of array1 are also in array2.",
"Array1 contains at least one element not in array2.")
joyce1
Hey Blair
Mr PoPoP
Andrea_A
T.O.
Hey Blair
Cool OOP.... Man I could Learn to Love The Enemy(OOP)... If I could Write Like That...... Spaghetti(Original poster) Above..... Pure OOP(Blair) Below..... You All Take A Look.....A Lot to Be Learned Here......
Cheers
Drew Speedie
I see no post marked as an answer so I'm going to assume that the advice so far has not answered the question. if you want to do as you originally asked and check whether all elements of one array are also in another array then you can do this:
Dim isSubset As Boolean = True For Each element As Object In array1 If Array.IndexOf(array2, element) = -1 ThenisSubset =
False Exit For End If Next element If isSubset ThenMessageBox.Show(
"All elements of array1 are also in array2.") ElseMessageBox.Show(
"array1 contains at least one element not in array2.") End IfAllighator
that being said. . .
the set could easily be extended to accomodate AddRange or a constructor with Array arguments.
for example, if this class were added
bewA
thx blair
i promise you will look at generics one day when i digest the basics first
best regards