URGENT Solution needed for resolving the issue of using existing hashtable for comparison purposes

Hi,

I have populated a hash table using a modified version of a solution in http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_22074905.html :

string result = GetResource ( "Digita.Importers.CoTax.PerTax.ISINlist.csv" ); //this reads the embedded resource fine
                  MatchCollection mc = Regex.Matches(result,
                        @"(?<isin>[^,]+),(?<company>[^,]+),(?<share>(?:[^\r]+))");

                  System.Collections.Hashtable table = new System.Collections.Hashtable();
                  
                                       
                  //this is one of the problems - no matches are made
                  foreach( Match m in mc)
                  {
                        string isin = m.Groups["isin"].Value.Replace("\r\n",string.Empty);
                        string company = m.Groups["company"].Value;
                        string share = m.Groups["share"].Value;
                   
                        //this also causes an error

                  
         
                        table[ isin ] =  isin;//new Values(isin, company, share) ;
                        table[company] = company;
                        table[share] = share;
}

in the DataRow called row in a populated DataSet called a, I want to use the above Hashtable for comparision purposes, in otherwords if any value in the table[company]  case-insensitively or phonetically matches the value in the row.ItemArray[2].ToString() (the company name), i want the specified strings populated as follows:

Company = the value of table[company],
strISIN = the value of table[isin],
Description = the value of table[share]

I have tried

foreach ( DataRow row in a.Tables[0].Rows )
                    {
                    if ( Regex.IsMatch(table[company].ToString(),quoteReplace ( row.ItemArray[2].ToString() ),RegexOptions.IgnoreCase))
                                             {
                                                  Company =  table[company].ToString()                                                   strISIN =  table[isin].ToString();
                                                  Description = table[share].ToString()                                              }
                                             else
                                             {
                         Company = row.ItemArray[2].ToString()
                                             }

to no avail. I urgently need to resolve this. Does anyone have suggestions?

JakeyCakesAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi JakeyCakes;

Here you go.

               //this is one of the problems - no matches are made
               foreach( Match m in mc)
               {
                    string isin = m.Groups["isin"].Value.Replace("\r\n",string.Empty);
                    string company = m.Groups["company"].Value;
                    string share = m.Groups["share"].Value;
                    table[ isin ] =  isin;//new Values(isin, company, share) ;
                    table[company] = company;
                    table[share] = share;
               }

The following lines marked with an * will fail because the use of the variables company, isin and
share is defined in different scope. The above use of company, isin and share are defined inside
the foreach code block and therefore can not be accessed from outside of the block and when the
code execution leaves that block of code those variables will be garbage collected.

            if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))
            {
*                  Company = DoubleQuote+ quoteReplace ( table[company].ToString() )+ DoubleQuote;
*                  strISIN = DoubleQuote+ quoteReplace ( table[isin].ToString() )+ DoubleQuote;
*                  Description = DoubleQuote+ quoteReplace ( table[share].ToString() )+ DoubleQuote;
            }
            else
            {
                  Company = DoubleQuote+ quoteReplace ( row.ItemArray[2].ToString() ) + DoubleQuote;
            }

The following code has been modify to make it work as long as all other variable used in the code
has been defined.                        

            string isin = "";
            string company = "";
            string share = "";
               //this is one of the problems - no matches are made
               foreach( Match m in mc)
               {
                    isin = m.Groups["isin"].Value.Replace("\r\n",string.Empty);
                    company = m.Groups["company"].Value;
                    share = m.Groups["share"].Value;
                    table[ isin ] =  isin;//new Values(isin, company, share) ;
                    table[company] = company;
                    table[share] = share;
               }


            if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))
            {
                  Company = DoubleQuote+ quoteReplace ( table[company].ToString() )+ DoubleQuote;
                  strISIN = DoubleQuote+ quoteReplace ( table[isin].ToString() )+ DoubleQuote;
                  Description = DoubleQuote+ quoteReplace ( table[share].ToString() )+ DoubleQuote;
            }
            else
            {
                  Company = DoubleQuote+ quoteReplace ( row.ItemArray[2].ToString() ) + DoubleQuote;
            }


Fernando
0
 
Fernando SotoRetiredCommented:
Hi JakeyCakes;

Sorry did not read the last message posted in the other question. See if this solves your problem.

Regex.IsMatch(table[company].ToString(),quoteReplace ( row.ItemArray[2].ToString() ),RegexOptions.IgnoreCase))

Where:
1       table[company].ToString() Is the string to be searched.
2       quoteReplace ( row.ItemArray[2].ToString() ) Is the string to match

Item 2 above may cause a no match to be found or a invalid match to be found. This will happen if the string that holds the match pattern, item 2 above, has any of the following characters. ==>  . $ ^ {  [ ( | ) * + ? \  <==. These characters have a special meaning in the Regex language and therefore needs to be escaped before using them. To escape the characters in the string do one of the following:

If using VS 2005

        string escapedStr = Regex.Escape(quoteReplace ( row.ItemArray[2].ToString() ))
        if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))


If using VS before 2005

        string escape = @"(\.|\^|\[|\{|\(|\||\)|\*|\+|\?|\\)";
        string escapedStr = Regex.Replace(quoteReplace ( row.ItemArray[2].ToString() ), escape, "\$1");
        if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))

That is the only thing that I can see that would cause it not to match correctly.

Fernando
0
 
JakeyCakesAuthor Commented:
Fernando, "\$1" threw an unrecognized escape sequence error
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Fernando SotoRetiredCommented:
Yes it would, make it @"\$1". Sorry about that.

Fernando
0
 
JakeyCakesAuthor Commented:
Fernando,

table[company].ToString(), or table[isin].ToString(), or table[share].ToString(),

throw the below errors:
The name 'company' does not exist in the class or namespace
The name 'isin' does not exist in the class or namespace
The name 'share' does not exist in the class or namespace
0
 
Fernando SotoRetiredCommented:
Is the following statements:

table[company].ToString(), or table[isin].ToString(), or table[share].ToString(),

Within the same code block that the company, isin and share were defined in?

Post the code block that the code appears in.

Thanks;
Fernando
0
 
JakeyCakesAuthor Commented:
Fernando, I can't past the full amount of code for data proctection purposes - however I hope what I can post helps.

string result = GetResource ( "Digita.Importers.CoTax.PerTax.ISINlist.csv" ); //this reads the embedded resource fine
                  MatchCollection mc = Regex.Matches(result,
                        @"(?<isin>[^,]+),(?<company>[^,]+),(?<share>(?:[^\r]+))");

                  System.Collections.Hashtable table = new System.Collections.Hashtable();
                  
                                       
                  //this is one of the problems - no matches are made
                  foreach( Match m in mc)
                  {
                        string isin = m.Groups["isin"].Value.Replace("\r\n",string.Empty);
                        string company = m.Groups["company"].Value;
                        string share = m.Groups["share"].Value;
                   
                        //this also causes an error

                  
         
                        table[ isin ] =  isin;//new Values(isin, company, share) ;
                        table[company] = company;
                        table[share] = share;

                  }

...followed by other code

foreach ( DataRow row in a.Tables[0].Rows )
                        {


                              ...
                              string escape = @"(\.|\^|\[|\{|\(|\||\)|\*|\+|\?|\\)";
                              string escapedStr = Regex.Replace(quoteReplace ( row.ItemArray[2].ToString() ), escape, @"\$1");
                              if ( Regex.IsMatch(table[1].ToString(), escapedStr, RegexOptions.IgnoreCase))
          {
                              
                              //                        if ( Regex.IsMatch(table[company].ToString(),quoteReplace ( row.ItemArray[2].ToString() ),RegexOptions.IgnoreCase))
                              //                        {
                                                            Company = DoubleQuote+ quoteReplace ( table[1].ToString() )+ DoubleQuote;
                                                            strISIN = DoubleQuote+ quoteReplace ( table[0].ToString() )+ DoubleQuote;
                                                            Description = DoubleQuote+ quoteReplace ( table[2].ToString() )+ DoubleQuote;
                                                      }
                                                      else
                                                      {
                              Company = DoubleQuote+ quoteReplace ( row.ItemArray[2].ToString() ) + DoubleQuote;
                                                      }
...
}
0
 
JakeyCakesAuthor Commented:
>>I can't past the full amount of code for data proctection purposes - however I hope what I can post helps.

I meant :

I can't paste the full amount of code for data proctection purposes - however I hope what I can post helps.

oops.
0
 
JakeyCakesAuthor Commented:
I also meant,

 if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))
          {
                         
                         //                    if ( Regex.IsMatch(table[company].ToString(),quoteReplace ( row.ItemArray[2].ToString() ),RegexOptions.IgnoreCase))
                         //                    {
                                                  Company = DoubleQuote+ quoteReplace ( table[company].ToString() )+ DoubleQuote;
                                                  strISIN = DoubleQuote+ quoteReplace ( table[isin].ToString() )+ DoubleQuote;
                                                  Description = DoubleQuote+ quoteReplace ( table[share].ToString() )+ DoubleQuote;
                                             }
                                             else
                                             {
                         Company = DoubleQuote+ quoteReplace ( row.ItemArray[2].ToString() ) + DoubleQuote;
                                             }
0
 
Fernando SotoRetiredCommented:
The variables isin, company and share are defined within the foreach loop below and therefore are not visible outside of the foreach loop.

               //this is one of the problems - no matches are made
               foreach( Match m in mc)
               {
                    string isin = m.Groups["isin"].Value.Replace("\r\n",string.Empty);
                    string company = m.Groups["company"].Value;
                    string share = m.Groups["share"].Value;
                   
                    //this also causes an error

               
         
                    table[ isin ] =  isin;//new Values(isin, company, share) ;
                    table[company] = company;
                    table[share] = share;

               }

The variables isin, company and share have no meaning in this block of code unless they have been defined somewhere before reaching this point. If you wish to use the above in the below code the must be define at a scope level the same as the below code or higher.

 if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))
          {
                         
                         //                    if ( Regex.IsMatch(table[company].ToString(),quoteReplace ( row.ItemArray[2].ToString() ),RegexOptions.IgnoreCase))
                         //                    {
                                                  Company = DoubleQuote+ quoteReplace ( table[company].ToString() )+ DoubleQuote;
                                                  strISIN = DoubleQuote+ quoteReplace ( table[isin].ToString() )+ DoubleQuote;
                                                  Description = DoubleQuote+ quoteReplace ( table[share].ToString() )+ DoubleQuote;
                                             }
                                             else
                                             {
                         Company = DoubleQuote+ quoteReplace ( row.ItemArray[2].ToString() ) + DoubleQuote;
                                             }

Fernando
0
 
JakeyCakesAuthor Commented:
>>If you wish to use the above in the below code the must be define at a scope level the same as the below code or higher.

Fernando, could you post me an example of what you mean?
0
 
JakeyCakesAuthor Commented:
Fernando,

excuse me whilst I kick myself very hard for missing something so simple....

The points are yours
0
 
Fernando SotoRetiredCommented:
That happens to all of us. Glad I was able to help, Have a great day. ;=)
0
 
JakeyCakesAuthor Commented:
Fernando, I have just realised something after awarding you the points: how could I modify the below so it compares all the company names not just the last company name ?

      string escape = @"(\.|\^|\[|\{|\(|\||\)|\*|\+|\?|\\)";
                              string escapedStr = Regex.Replace(quoteReplace ( row.ItemArray[2].ToString() ), escape, @"\$1");
                              if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))
          {
                              
                              //                        if ( Regex.IsMatch(table[company].ToString(),quoteReplace ( row.ItemArray[2].ToString() ),RegexOptions.IgnoreCase))
                              //                        {
                                                            Company = DoubleQuote+ quoteReplace ( table[company].ToString() )+ DoubleQuote;
                                                            strISIN = DoubleQuote+ quoteReplace ( table[isin].ToString() )+ DoubleQuote;
                                                            Description = DoubleQuote+ quoteReplace ( table[share].ToString() )+ DoubleQuote;
                                                      }
                                                      else
                                                      {
                              Company = DoubleQuote+ quoteReplace ( row.ItemArray[2].ToString() ) + DoubleQuote;
                                                      }
0
 
Fernando SotoRetiredCommented:
Hi JakeyCakes;

I guess you have a design issue here. You create a Hashtable and you place ID, Company Name and share as keys in the Hashtable with a value of the same thing. In this setup there is no way to identify which isin, company or share belongs to the same information

I am not sure what you are trying to accomplish in the below code. Can you explain what you want to accomplish here.

         string escape = @"(\.|\^|\[|\{|\(|\||\)|\*|\+|\?|\\)";
         string escapedStr = Regex.Replace(quoteReplace ( row.ItemArray[2].ToString() ), escape, @"\$1");
          if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))
          {                        
               Company = DoubleQuote+ quoteReplace ( table[company].ToString() )+ DoubleQuote;
               strISIN = DoubleQuote+ quoteReplace ( table[isin].ToString() )+ DoubleQuote;
               Description = DoubleQuote+ quoteReplace ( table[share].ToString() )+ DoubleQuote;
           }
           else
           {
               Company = DoubleQuote+ quoteReplace ( row.ItemArray[2].ToString() ) + DoubleQuote;
           }

Fernando
0
 
Fernando SotoRetiredCommented:
Hi JakeyCakes;

Here is some sample code that will retain the relationship with the 3 variables.

            System.Collections.Hashtable table = new System.Collections.Hashtable();
            
            public struct CompanyRec
            {
                  public string company;
                  public string isin;
                  public string share;
                  public CompanyRec( string Company, string Isin, string Share)
                  {
                        company = Company;
                        isin = Isin;
                        share = Share;
                  }
            }

Some where else in the code:

                  string isin = "";
                  string company = "";
                  string share = "";

                  foreach( Match m in mc)
                  {
                        isin = m.Groups["isin"].Value.Replace("\r\n",string.Empty);
                        company = m.Groups["company"].Value;
                        share = m.Groups["share"].Value;
                        table[company] = new CompanyRec(company, isin, share);
                  }
                  
Then to go through all of the companies you can do this

                  foreach( string comp in table.Keys )
                  {
                        CompanyRec cr = (CompanyRec) table[comp];
                        
                        Company = DoubleQuote+ quoteReplace ( cr.company )+ DoubleQuote;
                        strISIN = DoubleQuote+ quoteReplace ( cr.isin )+ DoubleQuote;
                        Description = DoubleQuote+ quoteReplace ( cr.share )+ DoubleQuote;

                        // Do something with the variables before looping to the next company.
 
                  }


Fernando
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.