|
Very cool Luc
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Its Looking Really cool,,thanks for Help but one thing more i have have string array collection, not char array collection,so how can i do this with string array collection..
|
|
|
|
|
if you want the result to be a char array, you have to turn my result into a char array;
I have already shown how that is done. It is just more of the same stuff.
|
|
|
|
|
Luc Pattyn wrote: this strange snippet
offers exactly that
neat
|
|
|
|
|
I'm glad I don't have to maintain that piece of code.
Curious to see what a LINQ-based solution would read like.
|
|
|
|
|
The good thing is it doesn't need any maintenance ; it could be implemented as an extension method to the string class, I would suggest it gets named RemoveNot(s1,s2): remove all chars from s1 that are not in s2.
No idea what LINQ can do here.
And also no idea what regex could do (I was surprised no one ever mentioned it so far).
I am pretty sure both of them would look even worse, and have lower performance.
A lambda expression might come in handy, haven't really considered it (am still working in .NET 2.0)
|
|
|
|
|
Luc Pattyn wrote: And also no idea what regex could do (I was surprised no one ever mentioned it so far).
Getting there, just have to read the replies so far first...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
This code will split the string, and give you the result as two arrays; one array with the regular split result, and one array with the splitters that was used.
The usedSplitters array is one item shorter than the words array. If no splitters were found, the words array contain the original string, and the usedSplitters array is empty.
string source = "asdf,asdf;;;asdf,asdf::asdf..asdf";
string[] splitters = { "..", ",", ";;;", "::" };
string pattern = "(" + string.Join("|", splitters.Select(s => Regex.Escape(s)).ToArray()) + ")";
MatchCollection matches = Regex.Matches(source, pattern);
string[] words = new string[matches.Count + 1];
string[] usedSplitters = new string[matches.Count];
int pos = 0;
for (int i = 0; i < matches.Count; i++) {
words[i] = source.Substring(pos, matches[i].Index - pos);
usedSplitters[i] = matches[i].Value;
pos = matches[i].Index + matches[i].Length;
}
words[matches.Count] = source.Substring(pos);
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I like the concept, my Visual Studio targetting 3.5 does not know the Select method though.
Anyway, it is not one of those obscure regex applications...
|
|
|
|
|
I think that you need using System.Xml.Linq; to get the Select extension.
Here's a way to create the pattern without it:
StringBuilder builder = new StringBuilder().Append('(');
bool first = true;
foreach (string s in splitters) {
if (first) {
first = false;
} else {
builder.Append('|');
}
builder.Append(Regex.Escape(s));
}
string pattern = builder.Append(')').ToString();
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
the original works like a charm once I added using System.Linq;
Without Select, I would write
StringBuilder builder = new StringBuilder().Append('(');
string sep="";
foreach (string s in splitters) {
builder.Append(sep).Append(Regex.Escape(s));
sep="|";
}
string pattern = builder.Append(')').ToString();
avoiding the extra state variable and if-else test.
Thanks.
|
|
|
|
|
Is it better? You are just replacing one state variable with another, making the value of the variable double as both state and value.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I trust this is subjective, however I see lots of small reasons, yes:
I find it shorter, more readable, less error prone, and possibly better for instruction scheduling.
Also for testing and proofing correctness avoiding if-tests generally is beneficial.
|
|
|
|
|
If you mean that you want to get the string that was originally split from the array, you will either have to write a method to do that, or you can use my handy-dandy string parsing class, available here[^] on CodeProject.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Have you had any good/bad experiences with Chilkat's .NET components? I'm specifically interested in their IMAP SDK but may go whole hog and buy their bundle (all components) if the general response is positive.
http://www.chilkatsoft.com/[^]
Thanks,
/ravi
|
|
|
|
|
Never used them or heard of them, Ravi.
|
|
|
|
|
Bummer - I was hoping they would be known to the CP community.
Their customer list, online docs, FAQ and free lifetime upgrade policy (they've been around since 2000) seem appealing. Too bad they're not as well known as I thought they'd be. Maybe it's the weekend.
/ravi
|
|
|
|
|
Hallo
i'd need to write a loop which iterate through the variables whose names start with a specific string: i.e.
for every variable whose name starts with "PATH_.." [or Left(name,5) == "PATH_"]
perform a file.exists check
or, again
for every variable whose name starts with "IMG_.."
do this: imgArray[variable_name] = variable_value
(imgArray being a pre defined Hashtable)
To be clearer, in PHP variable names can be obtained through the syntax $$variable, where $variable returns the variable value only.
In other words, i would like to automatically do a collection of variables grouped by particular strings in their names.
Is it possible?
|
|
|
|
|
To give the whole look, i'll try to explain what i want to do.
In the application, according to the user's choice, the main form backgroundImage changes, and is programmantically choosen among several external images whose paths are specified as user settings in the app.config: they are 10-15, number could change, those paths names all start with "PATH_".
So first of all i'd like to check if those files exists.
And then i'd like to change the main form background image at runtime according the current operation.
Maybe there's a different way to reach this?
|
|
|
|
|
Hi,
Operating on the names of variables is not possible in general; you can use reflection to operate
on the members (data fields, properties, methods, ...) of an object. But that is somewhat advanced stuff,
and not necessary for what you need.
I understand you will get the paths one by one from somewhere (app.config), and want to perform some
operations on them; hence I suggest you collect them in a Collection, probably a List< string> is
appropriate. You might first check the file existence, and only then insert the path name in the list.
Or you could insert the images themselves in the list; that would take more memory, but might better suits
your needs. Or you could create a little class that describes an image, holding its path name,
its image (initially null, until it is required, hence acting like a cache), and possibly some metadata.
|
|
|
|
|
Dear all,
I developed one application which collect the list of files in a directory after a comparison.
At the beginning I add the result into a datagridview but it seems I take too much virtual memory, for around 5 millions of file comparison.
I tried to write the list inside a text file but still the same problem.
So what I want to have is that how/what shoud I do to write the list of files after comparison directly into a text file (or list view) without taking too much memory.
Thanks for your reply
some piece of code would be appreciated.
|
|
|
|
|
It would appears that you're adding each filename to a list of some kind. If you don't want to use all that memory, you can't do that. Just write the information to the file as you find it, then don't add it to a collection of any kind.
|
|
|
|
|
Hi,
when you store all the file names somewhere (DataGridView, Array, List, ...) is does not scale well,
i.e. it works fine for small amounts, but starts to behave badly for large amounts; and disks could
hold millions of files. So try and avoid the need to have them all at once.
Example: say you want to compare the contents of two disks; there is NO need to first collect a list
of all the files, simply come up with a way to enumerate the files one by one in a predictable order,
now apply this same order to both disks. When they contain A+B+C+D+E+F+G and C+A+K+B+L you can't compare
them sequentially unless you enumerate in an ordered fashion, i.e. A+B+C+D+E+F+G and A+B+C+K+L,
these are easily comparable.
It you must have them all at once, try to keep the data to a minimum; for files and folders that
means don't hold the full path for each, just keep the name and a pointer to its parent. This may
save a factor of 2 to 5, which might be sufficient for your current needs, but again it does not
scale well, sooner or later you will run into trouble again.
|
|
|
|
|
Firstly, thanks for your prompt reply.
[Dave Kreskowiak]
Yeah this is what I want I think write the information directly as I find it, but I don't know how to make it.
[Luc Pattyn]
So you mean that I'll always get into trouble everytime I make my comparison ?
Let me explain to you first what my program do.
I have two servers with disk capacity around 7 To each.
The first one change everytime, 'cause it is like a transaction server, and second one is just for backing up the first one.
Let's say transaction server = 1A and backup server = 1B
Once someone made a transaction into 1A, it should be backed up to 1B but no need to delete files from 1B directly. Need to wait about two weeks and after that I should make a purge.
So my application is about purging files. Means that it compares first all files of the two servers and then lists those files before delete. It's at this level I'm into a trouble, making the comparison and then list all files.
So which solution should I adopt correctly ? Writing files directly inside a text file (by splitting if up to a specific size) or using a dictionnary or list... something like that. Or if have other solution I would appreciate it.
for information: my app is made with C# .NET 3.5
Thank you so much
|
|
|
|
|