Solved

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

Posted on 2006-11-29
16
197 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 62

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 62

Expert Comment

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

Fernando
0
 

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 62

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 62

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 62

Accepted Solution

by:
Fernando Soto earned 500 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 62

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 62

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 62

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now