Question Check part of a string in Hashtable Key

Anonymous

Well-known member
Joined
Sep 29, 2020
Messages
84
Programming Experience
Beginner
Can we check for a part of string in hashtable key? For example, our key is
C#:
key = residentnumber +","+ housenumber

I know have a function called containskey() but do we have something that could partially check for a string in a hashtable key?
For example, if we could search
C#:
if(key.cotains(housenumber))

My problem statement
I have two .csv files -

File1

C#:
EmployeeId  Username         BranchId   NewJoinee EmpType  AccessDetails
5543101    abc.username      40000        0        64     60060||60060
5543102    someone.username  40000        0        64     52474||52474
5543103    ABC.someone       40000        0        NULL     NULL
5543104    XZ.someone        40000        0        64      pppA082||

The Access Details column contains details as ``CardNumber|SecretPin|TokenNumber```

Same is the File2
C#:
EmployeeId  Username         BranchId   NewJoinee EmpType  AccessDetails
5000101    abc.username      40500        0        64     5555||60060
5000102    someone.username  40500        0        64     52474||1234
5000103    ABC.someone       40500        0        NULL     NULL
5000104    XZ.someone        40500        0        64      10A082||5644

I have to compare these two files, my conditions are -

  • If the card number matches but token Id doesn't, do nothing
  • If the card number does not match but token Id matches, do nothing
  • If the access details are null, then print this employee details to another file
  • If both card number and token Id do not match, then print this employee details to another file
I am currently doing this but using this approach I am only able to implement the 1st condition.I am using hashtables to store the content of files. I am making cardnumber as key and rest of the details as value.

This is how I am storing values of File1

C#:
if (!_File1.ContainsKey(cardnumber))
 _File1.Add(cardnumber, username + "," + branchId + "," + employeeID + "," + cardnumber + "," + tokennumber + "," + empType + "," + newJoinee);

In the simiar fashion,I am storing the contents of file2 in a different hashtable. Then I am doing the following to read file2 data and compare



C#:
//readingFile2 data
row = SplitCSV(line);

employeeID = row[employeeIndex].Trim().ToString();
username = row[UsernameIndex].Trim().ToString();
branchID = row[branchIdIndex].Trim().ToString();
newJoinee = row[njIndex].Trim().ToString();
emptype = row[emptypeIdIndex].Trim().ToString();
accessdetails = row[ValueIndex].Trim().ToString();

if (!accessdetails.Equals("NULL"))
{
    string[] values = value.Split('|');
    cardnumberfile2 = values[0];
    tokenumberFile2 = values[2];
}
else
{
    cardnumberfile2 = employeeID;
    tokennumberfile2 = "";
}

if(_file1.ContainsKey(cardnumberfile2))
{
   //reading values from File1
 
   // As cardnumber in file2 is present in the file1, then do nothing
}

I was wondering of perhaps I should store key as
C#:
key = cardnumber + "," + tokennumber

and then perhaps do something that could search if a token Id or a cardnumber is present in this key or not. because in the above approach, I can test if the cardnumber is present or not because it is the key in both Files, but this approach will fail if we have different cardnumbers and same token number(row 1 in the example table)

Secondly, I dont know how to deal with the null value column as there are no unique linking values for these records.

P.S - I don't own the data. I can't change it.
 
No. That defeats the point of a hashtable. A hashtable is a table of hashes. Given a key, it gets hashed and then that hash is located in the table. You can't look for part of a key because the key itself is not in the hashtable; the has of the key is. You'll just have to treat the keys as a collection of strings and search that like you would any other collection. It's easy enough, but won't benefit from the performance gains that hashtables provide.
 
Can we check for a part of string in hashtable key?
No; you store multiple keys if you want to answer that one

C#:
cardtokenkey = cardnumber + "," + tokennumber;
cardkey = cardnumber + "," ;
tokenkey = "," + tokennumber;

if(!dictionary.ContainsKey(cardtokenkey)){
 if(dictionary.ContainsKey(cardkey))
  //then it is the token that does not match
 else if(dictionary.ContainsKey(tokenkey))
  //then it is the card that does not match
 else
  //then neither matches
}

note: this relies on card and token never having a comma in them as data. They look numeric to me, but the logic is defeated if a token could start with a comma, and you'd have to use something else that would never be in a token..
Or have multiple hashtables, one for card+token, one for card, one for token
 
Back
Top Bottom