Resolved Best Syntax for input / outcome process?

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
Sorry for the title, I think if I knew how to ask this question, I would likely know the answer to the question.

The process I had in mind is of a group of names:

William Bill Billie Billy Will Willy

If any one of those is entered, then the remaining list is output. I have a lot of names I want to do this for, not just this one.

I was thinking about a table or database, but the problem is that some names have far fewer variations than others. Ranging from just a couple (Steven) to in excess of 10 (John)

Was wondering if there was an easy syntax method by which any variable within a group of variables being input would result in the remaining variables being output? That seems like it is something that someone would have wanted to do before now.

Hoping there is something I have not come across yet, if anyone can point me towards it by giving it a name that would be helpful. Or any ideas of how to do this?
 
Solution
Assuming you had a List<List<string>> where each of the inner lists contained the names and their equivalent nicknames, it's pretty easy to find a matching name and print out its equivalent names:
C#:
var allNames = new List<List<string>>();
allNames.Add(new List<string>() { "William", "Bill", "Billie", "Billy", "Will", "Willy" });
allNames.Add(new List<string>() { "Steven", "Steve", "Boots" });
var input = Console.ReadLine();
var equivalentNames = allNames.Where(n => n.Contains(input)).Select(n => n.Except(input)).FirstOrDefault();
if (equivalentNames?.Count() > 0)
    Console.WriteLine(string.Join(", ", equivalentNames));

Now as for storing the names that are equivalent, JSON should readily let you store something like...
Assuming you had a List<List<string>> where each of the inner lists contained the names and their equivalent nicknames, it's pretty easy to find a matching name and print out its equivalent names:
C#:
var allNames = new List<List<string>>();
allNames.Add(new List<string>() { "William", "Bill", "Billie", "Billy", "Will", "Willy" });
allNames.Add(new List<string>() { "Steven", "Steve", "Boots" });
var input = Console.ReadLine();
var equivalentNames = allNames.Where(n => n.Contains(input)).Select(n => n.Except(input)).FirstOrDefault();
if (equivalentNames?.Count() > 0)
    Console.WriteLine(string.Join(", ", equivalentNames));

Now as for storing the names that are equivalent, JSON should readily let you store something like this.
C#:
{
    allNames : [
        [ "William", "Bill", "Billie", "Billy", "Will", "Willy" ],
        [ "Steven", "Steve", "Boots" ]
    ]
}
 
Last edited:
Solution
Cheers Skydiver, I have been reading up on Linq and I had recently read of the "n.Except(input))" I was just messing around with:

C#:
IEnumerable<string> nameQuery =
    from names in cleanNames
    where names != otherNames
    select names;

Don't get me wrong, that was never going to get me there ....but it is entertaining reading.

Not sure I would have been able to put it all together as you have here though, so appreciate you taking the time.

I had considered lists, but didn't think of a List<list<string>> and thought having 100s of Lists would have been a poor way to do it, but having lists nested within lists....now that makes sense.
 
Ok had a bit of a problem with this line:

C#:
allNames.Where(n => n.Contains(input)).Select(n => n.Except(input)).FirstOrDefault();

Getting a convoluted error:

List<string>' does not contain a definition for 'Except' and the best extension method overload 'Queryable.Except<char>(IQueryable<char>, IEnumerable<char>)' requires a receiver of type 'IQueryable<char>'

I had a good play around for the last hour, but I can't get it to work.
 
Did you include System.Linq in the using's?
 
Ah. I see. Except() wants a list. You need to change over to using Select() and pick the items not equal to the input.
 
Got it, and honestly, I don't think I needed to exclude the name from the List.

I can just not pass through the input, and just keep the full list - which is the same thing.

Besides, I distinct before the final output due to other things that are being done.

Thanks Skydiver.
 
Back
Top Bottom