-4

I create a list<string> machineTypes and fill it with data. I want to check to see if the collection contains any combination of strings. My initial plan was to use a for loop, but obviously I can't check multiple indexes in the middle of a for loop.

for (int i = 0; i < machineTypes.Count; i++)
            {
                if (machineTypes[i] == "W")
                //do stuff
                if ((machineTypes[i] == "P") && (machineTypes[i] == "W") && (machineTypes[i] == "A") && (machineTypes[i] == "C"))
                    //do stuff
            }

So I'm looking for suggestions as to the best way to do this. I suppose I could use String.Join, but I was wondering if there was a more elegant way.

user1729696
  • 311
  • 2
  • 5
  • 16
  • 5
    How `machineTypes[i]` can be `P`, `W`, `A` and `C` at the same time...? – Konrad Kokosa Nov 06 '14 at 20:34
  • 3
    Can you give more of an overview of what data is in each item inside of machineTypes, and a better explanation of what condition you are trying to check for? – lukevp Nov 06 '14 at 20:35
  • Please define "combination of strings". What input do you anticipate? What condition are you actually looking for? – Peter Duniho Nov 06 '14 at 20:36
  • Konrad, that is what I stated in my question. lukevp, the data in each item is a string. The conditional is the value of the string. I'm not sure what other information you could possibly want. – user1729696 Nov 06 '14 at 20:36
  • 1
    Do you actually mean `machineTypes.Contains("P") && machineTypes.Contains("W") && ...` ? – pescolino Nov 06 '14 at 20:37
  • 1
    Give a sample: input, expected output. This will be a good start. – Patrice Gahide Nov 06 '14 at 20:38
  • what do you mean by `contains any combination of strings`? Because your code doesn't reflect anything similar to that – Jonesopolis Nov 06 '14 at 20:38
  • @pescolino that would work. Might get ugly after a few conditionals, but that's fine. – user1729696 Nov 06 '14 at 20:40
  • If you need to check if all strings in your "other" set present in original - check the linked [duplicate](http://stackoverflow.com/questions/332973/linq-check-whether-an-array-is-a-subset-of-another), if you need to check if [Any](http://msdn.microsoft.com/en-us/library/vstudio/bb534972(v=vs.100).aspx) string in "other" set present in original - use something like `machineTypes.Any(c => other.Contains(c))` – Alexei Levenkov Nov 06 '14 at 20:53
  • @AlexeiLevenkov you have incorrectly marked this as a duplicate. This has nothing to do with subsets. – user1729696 Nov 06 '14 at 21:03
  • The thing is, I think that few people understand what this has to do with anything at all. You give no sample input/output, you don't really answer to comments, your code is silly as it is (as stated in the first comment), and you blame people for guessing? – Patrice Gahide Nov 06 '14 at 21:19

1 Answers1

0

Perhaps not too elegant - but something like this might help

for (int i = 0; i < machineTypes.Count; i++)
            {
                int jj=i;
                if (machineTypes[i] == "W")
                //do stuff
                if (jj< machineTypes.Count-4)
                if ((machineTypes[jj] == "P") && (machineTypes[jj+1] == "W") && (machineTypes[jj+2] == "A") && (machineTypes[jj+3] == "C"))
                    //do stuff
            }

I just made a new var jj in case you want to increment or alter its value, without altering the i value. Note the if (jj< machineTypes.Count-4) condition checking to see that you can safely use jj+3 as an index.

Grantly
  • 2,546
  • 2
  • 21
  • 31
  • That is far more complicated than it needs to be – Jonesopolis Nov 06 '14 at 20:41
  • Yes I know, but that was for ease of understanding, and more complex enhancement. The question was not too clear, so I allowed for the use of jj++, etc – Grantly Nov 06 '14 at 20:42
  • Ah, unfortunately I would have no idea how many values or at what index the value would be stored. It's an interesting technique though. I'll give you a +1 for the effort. – user1729696 Nov 06 '14 at 20:43
  • I have no idea how this code is related to the question (which I don't understand either). You sure need a twisted brain to crack that. – Patrice Gahide Nov 06 '14 at 20:46
  • Without knowing how many values, it gets very difficult. Unless you know how many values - at some stage (perhaps when you have found them all) - then you can still check your index using something like `if ( (index – Grantly Nov 06 '14 at 20:47
  • Although using the built in comparisons would be better. If you need to make a new temporary list starting at your desired index, then use Except on the temp list and your list of values. See http://stackoverflow.com/questions/12795882/quickest-way-to-compare-two-list – Grantly Nov 06 '14 at 20:54