Have you thought about using a regular expression Look up:
System.Text.RegularExpressions
with classes like RegEx, you can basically loop through your list and check for a match, here's the example from MSDN:
public class Test {
public static void Main () {
// Define a regular expression for currency values. Regex rx = new Regex(@"^- \d+(\.\d{2}) $");
// Define some test strings. string[] tests = {"-42", "19.99", "0.001", "100 USD"};
// Check each test string against the regular expression. foreach (string test in tests) { if (rx.IsMatch(test)) { Console.WriteLine("{0} is a currency value.", test); } else { Console.WriteLine("{0} is not a currency value.", test); } }
}
}
The hard part is writing regexs but there are lots of websites on it also find a tool called "RegexDesigner.NET" which makes writing them much easier.
Btw the regex you'd use would be something like "\\\\server\\share\\\w{1}\\\z" but you'll have to play around to get it completely right.
And it could go recursively to find for instance these (2 iterations based on predicate = \\server\share\b\ \):
\\server\share\b\n \\server\share\b\m
again, each only once.
I am now looking into the List.findAll() method, it works with something called predicates and/or delegates. I'm having a hard time understanding it, so any help would be very much appreciated.
heres wate i would do but im not positive because i dont know all of your code i would have to see it.
I would first call this serve share folder letter from a method and send the letter your looking for FOR INSTANCE. (Quick Example)
// get folder "a"
string name = "a"
folderLetter( name )
public void folderLetter( string letter)
If( letter.StartWith("a") )
Execude the command.... if you would like i could make a quick demo program similar to what i think you are trying to do and email it to you if you would like. but try doing the foreach and in soide the look get the letter and if its a string value to the .StartsWith property. If your using an array list you have to typecats the letter back to a string because array list only hold general objects
( (string)List[1] ).Name.StartWith() try this may also. i need mofr detail to be more specific on what you can do..
You can implement the IEnumerator interface, you return an enumerator that only iterates over the people that have a name starting with an 'A'. Read more about it here:
Thanks everyone! It's not so much about how to find what I'm looking for. I use regular expressions for that. It's just that is was wondering how to do that in a loop over a List. What I would really like to see, is a method for a List like FindAllUnique(). Right now List has methods Exists(), Find and FindAll.
So from a list with 1, 2, 3, 4, 6, 6, 7, 7, 8, 9 you could do a FindAll() looking for all even numbers, the result would be 2, 4, 6, 6, 8. What I would like is a method that would return 2, 4, 6, 8 So only unique occurences. Right now, I have to filter the result from FindAll().
I used numbers this time, since it was not about the data, but more about the most elegant method to get the data you want. SQL has the DISTINCT operator for this.
Download Wintellect's free PowerCollections library (assuming you're using C# 2.0). You have to register but registration is free. It has algorithms for this kind of thing.
foreach over an array, can I specify a predicate
mattneck
Hope this helps a bit.
tolsen64
Have you thought about using a regular expression Look up:
System.Text.RegularExpressions
with classes like RegEx, you can basically loop through your list and check for a match, here's the example from MSDN:
public class Test
{
public static void Main ()
{
// Define a regular expression for currency values.
Regex rx = new Regex(@"^- \d+(\.\d{2}) $");
// Define some test strings.
string[] tests = {"-42", "19.99", "0.001", "100 USD"};
// Check each test string against the regular expression.
foreach (string test in tests)
{
if (rx.IsMatch(test))
{
Console.WriteLine("{0} is a currency value.", test);
}
else
{
Console.WriteLine("{0} is not a currency value.", test);
}
}
}
}
The hard part is writing regexs but there are lots of websites on it also find a tool called "RegexDesigner.NET" which makes writing them much easier.
Btw the regex you'd use would be something like "\\\\server\\share\\\w{1}\\\z" but you'll have to play around to get it completely right.
Chris
raphts
\\server\share\a
\\server\share\a\i
\\server\share\a\j
\\server\share\b
\\server\share\b\m
\\server\share\b\n\x
\\server\share\b\n\y
\\server\share\c
\\server\share\d
I need to do a foreach loop over this list, that loops over unique paths that look like: \\server\share\ \ where can be anything
It would find these this, and all of them only once (4 iterations based on predictate = \\server\share\ \):
\\server\share\a
\\server\share\b
\\server\share\c
\\server\share\d
And it could go recursively to find for instance these (2 iterations based on predicate = \\server\share\b\ \):
\\server\share\b\n
\\server\share\b\m
again, each only once.
I am now looking into the List.findAll() method, it works with something called predicates and/or delegates. I'm having a hard time understanding it, so any help would be very much appreciated.
.xXDaveXx.
heres wate i would do but im not positive because i dont know all of your code i would have to see it.
I would first call this serve share folder letter from a method and send the letter your looking for FOR INSTANCE. (Quick Example)
// get folder "a"
string name = "a"
folderLetter( name )
public void folderLetter( string letter)
If( letter.StartWith("a") )
Execude the command.... if you would like i could make a quick demo program similar to what i think you are trying to do and email it to you if you would like. but try doing the foreach and in soide the look get the letter and if its a string value to the .StartsWith property. If your using an array list you have to typecats the letter back to a string because array list only hold general objects
( (string)List[1] ).Name.StartWith() try this may also. i need mofr detail to be more specific on what you can do..
Ramaj
Read more about it here:
- Using IEnumerator and IEnumerable in the .NET Framework
- IEnumerable And IEnumerator interfaces
But i think that is an lot of overhead. You can better just compare the iteration yourself:foreach( Human human in people )
{
if( !human.FullName.StartsWith( "A" ) )
{
// Name didn't start with an A so just continue.
continue;
}
// TODO: Implement logic.
}
Jayender .v s
So from a list with 1, 2, 3, 4, 6, 6, 7, 7, 8, 9 you could do a FindAll() looking for all even numbers, the result would be 2, 4, 6, 6, 8. What I would like is a method that would return 2, 4, 6, 8 So only unique occurences. Right now, I have to filter the result from FindAll().
I used numbers this time, since it was not about the data, but more about the most elegant method to get the data you want. SQL has the DISTINCT operator for this.
Scott52
Download Wintellect's free PowerCollections library (assuming you're using C# 2.0). You have to register but registration is free. It has algorithms for this kind of thing.
e.g., Algorithms.RemoveDuplicatesInPlace()