views:

54

answers:

1

I have an array {{".txt|.doc|.docx", "100000"}, {".xls|.xlsx|.xxx" , "10000"}, ".npp", "100000"}

I am trying to find a way to run a foreach loop on each member of the array in a loop while also checking the first member as a string not an array.

The code will search for all docs greater than 10000 bytes, as long as the docs are txt, doc, docx, etc and then search all xls, and then npp. Some of the members of the first part of the array have multiple types, some just one. Is there an efficient way to do this?

I tried: if (size == (size[2,i]) && ((ext != sextension[1, 0].ToUpper())) && ((ext != sextension[1, x].ToUpper())))

but obviously that doesn't work. Sorry if this is not clear, but I am not sure how to explain it otherwise.

A: 

here is the code you put in the comment above:

c# string[,] extension = new string[5,2]{ 
  {".doc|.docx|.doc1","1000"} , 
  {".xls", "1000"} ,
  {".pdf","1000"},
  {".zip", "10000"}, 
  {".7z|.pz", "100000"} };

foreach (string p in array) 
{ 
  if (p == extension[0, 1]()) || ..ext2 = size or ..ext3 && (size) 
  {
     Console.WriteLine("Ext {0} has a file size equal to {1}", size_entention, size);

Does not make a whole lot of sense, but I will go for it (guessing all the way):

The best way to do the extension match given your data structure is to use regEx.Match(), however this can be slow. (Remember if your search string is .docx you will need to make it @"\.docx" before calling regEx.match() -- . is reserved in regEx.

I don't suggest this, it is slow. What you really want is a dictionary. Here is an example of using dictionary:

Dictionary<string, int> d = new Dictionary<string, int>();
d.Add(".doc", 1000);
d.Add(".docx", 1000);
d.Add(".doc1", 1000);
d.Add(".xls", 1000);
d.Add(".pdf", 1000);
d.Add(".zip", 10000);
d.Add(".7z", 100000);
d.Add(".pz", 100000);

foreach (string p in array) 
{ 
  if (d.ContainsKey(p)
    Console.WriteLine("Ext {0} has a file size equal to {1}",p, d[p]);
}

This will be much faster on the search that what you were doing. Setting up the dictionary is slow so I would suggest caching it.

Hogan
I apologize if I am not coming across clear. The code I have isn't working, so that is why it doesn't make sense. I am trying to search a file system for all files with an extension, but for the same type of files (doc, docx, etc) I want to only compare once. The problem I am running into is when I search for array[0,0] filetypes with size array[0,1] (in a loop) I want the if statement to compare array[0,0] (all of them after I split them).So if file .docx || .doc || .rdoc == filesize then console.writeline.
KHT
use regex then, like I said -- it is a worse choice for whatever you are doing (which is still not clear).
Hogan
note this : the example code above will do exactly what your example was trying to do -- try it in your code and see if it is actually what you want.
Hogan