Results 1 to 5 of 5

Thread: [RESOLVED] Logic Help with iteration through arrays.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Resolved [RESOLVED] Logic Help with iteration through arrays.

    Hey,

    I am breaking my head here because this seems very easy but I cant seem to figure it out.

    I have an array (100,3)

    Which follows something like this

    Code:
    {Bob},{Glass},{Var}
    {Bob},{Water},{Var}
    {Cedar},{Steel},{Var}
    {Frank},{Wood},{Var}
    .....
    The names are in alpha order and names can repeat. How would I go about displaying the Names without repetition

    So if looking at the example above, the output would be

    Code:
    Bob - 2
    Cedar - 1
    Frank - 1
    Thanks

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Logic Help with iteration through arrays.

    Is this actually a case of:

    Code:
    Bob -> - Glass = Var
           - Water = Var
    Cedar -> - Steel = Var
    Frank -> - Wood = Var
    ?

    That is, each distinct value in the first "column" represents one "thing" (person?), and each row they appear on is an entry in a key/value collection. "Bob" has two key/values in his collection, Cedar and Frank both have one?

    If that's the case, start modelling that! Create a Person (?) class that has a Dictionary(Of String, String) (or whatever is appropriate), then you have (in the above example) three instances of Person (note: not 4!)

    Whilst you could do what you're asking with some Linq and GroupBy, it's probably not a good idea.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Logic Help with iteration through arrays.

    To add to what EG said in a more general way: There are only a few cases where a multi-dimensional array makes sense. In all other cases you want to use a class. The only place where multi-dimensional arrays generally make sense are for laying out grids for display. Since this case doesn't appear to have anything to do with a grid, it almost certainly would benefit from a different design based on classes. You have, yourself, already stated the key reason: It's hard to get your head around multi-dimensional arrays. They are also hard to work with in other ways, too, since you can't easily expand any dimension other than the last one, and even that one doesn't expand very efficiently (though it is undeniably easy). In this case, it's hard to even understand the purpose of the array just from looking at it, as EG showed by not being clear on what was described. After all, you say it is two dimensional, yet you show some third item using a syntax reminiscent of array syntax. Alternatively, it looks like some kind of jagged array. In any case, a class is going to be much easier for you to work with, and for you to understand. However, you might not go with a person class if that is all the data you have. Instead, you might consider something like this:

    Dictionary (of String, Dictionary(of String, Integer))

    It's an odd looking thing, but simple enough to use. The outer key is the person name, so there would be keys for Bob, Cedar, and Frank. The value associated with each of these keys would be Dictionary of whatever those other items are (e.g. Glass, Water, Steel, Wood). I used a string as that inner key, but those really don't look like they are all that open ended, so I might be tempted to make up an Enum called ResourceType (or whatever is more appropriate), with values Glass, Water, Steel, Wood, etc., and use that Enum as the key for the inner dictionary. Therefore, each person would have a dictionary with whatever items they have, and the corresponding values. That's going to be easier to understand than a mutlidimensional array, since you can work with the dictionary keys, which will always be recognizable. The outer key will be the name, and each name will have a dictionary associated with it keyed on item type. This would be even easier than a class as long as nothing more than accessing/updating these items is required. If more is required, then the class that EG suggested would be better.
    My usual boring signature: Nothing

  4. #4
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Logic Help with iteration through arrays.

    if you insist on your current line of code try this

    dim list as list(of string)

    then a simple loop

    for i = 0 to array.ubound(0) -1
    if not list.contains(array(i,0).tostring) then list.add(array(i,0).tostring)
    end if

    this will list each name only once in the list
    theres many ways to do it, this is one of them,
    i always get told to use dictionary though like they mentioned above
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Logic Help with iteration through arrays.

    Quote Originally Posted by Shaggy Hiker View Post
    To add to what EG said in a more general way: There are only a few cases where a multi-dimensional array makes sense. In all other cases you want to use a class. The only place where multi-dimensional arrays generally make sense are for laying out grids for display. Since this case doesn't appear to have anything to do with a grid, it almost certainly would benefit from a different design based on classes. You have, yourself, already stated the key reason: It's hard to get your head around multi-dimensional arrays. They are also hard to work with in other ways, too, since you can't easily expand any dimension other than the last one, and even that one doesn't expand very efficiently (though it is undeniably easy). In this case, it's hard to even understand the purpose of the array just from looking at it, as EG showed by not being clear on what was described. After all, you say it is two dimensional, yet you show some third item using a syntax reminiscent of array syntax. Alternatively, it looks like some kind of jagged array. In any case, a class is going to be much easier for you to work with, and for you to understand. However, you might not go with a person class if that is all the data you have. Instead, you might consider something like this:

    Dictionary (of String, Dictionary(of String, Integer))

    It's an odd looking thing, but simple enough to use. The outer key is the person name, so there would be keys for Bob, Cedar, and Frank. The value associated with each of these keys would be Dictionary of whatever those other items are (e.g. Glass, Water, Steel, Wood). I used a string as that inner key, but those really don't look like they are all that open ended, so I might be tempted to make up an Enum called ResourceType (or whatever is more appropriate), with values Glass, Water, Steel, Wood, etc., and use that Enum as the key for the inner dictionary. Therefore, each person would have a dictionary with whatever items they have, and the corresponding values. That's going to be easier to understand than a mutlidimensional array, since you can work with the dictionary keys, which will always be recognizable. The outer key will be the name, and each name will have a dictionary associated with it keyed on item type. This would be even easier than a class as long as nothing more than accessing/updating these items is required. If more is required, then the class that EG suggested would be better.
    Sorry for the late reply.

    While I agree have the data put into classes would be a hell of a lot easier, the array is not created by me. It is created in another program and through a COM interface it is passed to me and it would not make sense to iterate through that array as it was just being used to fill the contents of a table quicky.

    So after a coffee and waking up, I ended up using a for loop like so

    vb.net Code:
    1. for x = 0 to array.length
    2.    
    3.     if array(x,0) = array(x+1,0) then
    4.          namecount = namecount + 1
    5.          list = list & "," & array(x,1)
    6.          skip = true
    7.     else
    8.          skip = false
    9.     End If
    10.        
    11.    If not skip then
    12.  
    13.      'Did what i needed to do here with the count and name :)
    14.  
    15.    End if

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width