?
Solved

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

Posted on 2006-11-29
16
Medium Priority
?
206 Views
Last Modified: 2011-09-20
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?

0
Comment
Question by:JakeyCakes
  • 8
  • 8
16 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18037000
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
 

Author Comment

by:JakeyCakes
ID: 18037034
Fernando, "\$1" threw an unrecognized escape sequence error
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18037094
Yes it would, make it @"\$1". Sorry about that.

Fernando
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:JakeyCakes
ID: 18037221
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18037304
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
 

Author Comment

by:JakeyCakes
ID: 18037381
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
 

Author Comment

by:JakeyCakes
ID: 18037389
>>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
 

Author Comment

by:JakeyCakes
ID: 18037412
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18037561
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
 

Author Comment

by:JakeyCakes
ID: 18037654
>>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
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 18037862
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
 

Author Comment

by:JakeyCakes
ID: 18037988
Fernando,

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

The points are yours
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18038054
That happens to all of us. Glad I was able to help, Have a great day. ;=)
0
 

Author Comment

by:JakeyCakes
ID: 18038055
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18038462
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
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18038930
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question