Solved

Search Multidimensional Array for Specific Column Value

Posted on 2014-01-23
4
401 Views
Last Modified: 2014-01-23
I have the following array:

      // Declare array  
        public string[,] arrDeck = new string[3, 4];
 
        // Constructor
        public MainPage()
        {
            InitializeComponent();

           // populate multidimension array (two dimensions)
            arrDeck[0, 0] = "a2";
            arrDeck[0, 1] = "This is not a recommended starting hand";
            arrDeck[0, 2] = "5.4 Pct";
            arrDeck[0, 3] = "153";

            arrDeck[1, 0] = "a2s";
            arrDeck[1, 1] = "You need 3 callers in an unraised pot. You need 4 callers in a raised pot.";
            arrDeck[1, 2] = "12.7 Pct";
            arrDeck[1, 3] = "39";
        }

How do I search to find "a2s" ?

Thanks,
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 300 total points
ID: 39803275
Maybe something like this:
        /// <summary>
        /// Search 2 dimensional array for a specific string in a specific position
        /// </summary>
        /// <param name="arr">the array to search through</param>
        /// <param name="fnd">the string to find</param>
        /// <param name="ps">the position (2nd array dimension) where the string must be located</param>
        /// <returns>the index (1st dimension) where it was found, or -1 if not found</returns>
        protected int getIndex(string[,] arr, string fnd, int ps) {
            if (ps >= 0 && ps <= arr.GetUpperBound(1)) {
                int p = -1;
                while (++p <= arr.GetUpperBound(0)) {
                    if (arr[p, ps] != null && arr[p, ps].Equals(fnd))
                        return p;
                }
            }
            return -1;
        }

Open in new window

Example for calling code:
            int i = getIndex(arrDeck, "a2s", 0);
            string message;
            if (i >= 0) {
                message = arrDeck[i, 1];
            } else {
                message = "not found";
            }

Open in new window

I chose to return index only because you may want to reference the other items from the same calling code as well.
0
 

Author Comment

by:Dovberman
ID: 39803637
I have never used this technique before.  It seems more efficient than a For each or Do Until loop.  I will step through it to get a better understanding.

Thanks,
0
 

Author Comment

by:Dovberman
ID: 39803677
Yes, the row index is what I needed.  Then I can get other columns from the found row.
Thank  you,
0
 

Author Closing Comment

by:Dovberman
ID: 39803683
Yes, the row index is what I needed.  Then I can get other columns from the found row.
Thank  you,
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

710 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