Finding *unique* entries in a list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi.

I'm fairly new to C#, and have had some luck getting it to do what I want
to, but something I could do with bash shell scripting is escaping me.

Bash has a tool called "uniq", and it can take a list and just take out
unique entries. I am trying to create something that will take a list of Zip
files whose names can be similar (groups of files such as
xlsfile1.zip....xlsfile9.zip) and allow me to group them together in some way
to use in file operations.

My hope is that I can move files that are alike into a folder called
"xlsfile" and then use something to unzip *.zip.

I know this wouldn't be an issue if I always knew what would be the same,
but since that's always different, I need some generic way to *sense* what
parts are the same but completely unique from unrelated files.

I'm sorry if this is convoluded or makes little sense, but I just didn't
know how else to describe my issue.

Thanks
 
To my opinion in order of this to work you have to know what is the common
part of the filenames. For instance it is always a name followed by some
number. You could use Regular Expressions to filter the distinct names.

\w*\d*\.zip : Any number of charachters followed by any number of digits
followed by .zip.
 
A simple solution is creating an empty list, iterating over your items in
the list of items and, by using a Hashtable, do lookups for each entry and
only insert unique items to the new list.

Better yet, if you control all inserts and deletes, you could ensure that
the list is always has unique items.

Coding a UniqueList using a IComparer, should not take many minutes..

- Michael S
 
: [...]
: I'm sorry if this is convoluded or makes little sense, but I just didn't
: know how else to describe my issue.

We're all programmers here, so the clearest way to describe your issue
would be to give a shell script that does what you want! :-)

Greg
 
: A simple solution is creating an empty list, iterating over your items
: in the list of items and, by using a Hashtable, do lookups for each
: entry and only insert unique items to the new list.

Why bother with the extra check? Simpler code, i.e., unconditional
Add, produces the same result.

Greg
 
Greg Bacon said:
Why bother with the extra check? Simpler code, i.e., unconditional
Add, produces the same result.

Greg

I think that's exactly what I wrote.

- Michael S
 
: :
: > In article <[email protected]>,
: > Why bother with the extra check? Simpler code, i.e., unconditional
: > Add, produces the same result.
:
: I think that's exactly what I wrote.

I translated
A simple solution is creating an empty list, iterating over your
items in the list of items and, by using a Hashtable, do lookups
for each entry and only insert unique items to the new list.

to code such as

ICollection uniq1(string[] a)
{
ArrayList unique = new ArrayList(); // create empty list

Hashtable seen = new Hashtable();
foreach (string s in a) // iterate over list of items
{
if (!seen.Contains(s)) // lookup for each entry
{
seen = null;
unique.Add(s); // only insert unique items
}
}

return unique;
}

By "simpler code," I was thinking of

ICollection uniq2(string[] a)
{
Hashtable seen = new Hashtable();
foreach (string s in a)
seen = null;

return seen.Keys;
}

Greg
 
Back
Top