2

Related, but not Duplicate (it's a completely different language, PHP not C#):
How to create multi-dimensional array from a list?

How would I go about converting a list of 'Tuple's to string[,]?

This is part of a web crawler, but i'm messing with conversions of lists and arrays just out of curiosity. Here's the method.

private string[,] getimages(string url)
    {
        List<Tuple<string, string>> images = new List<Tuple<string, string>>();
        string raw = client.DownloadString(url);
        while (raw.Contains("<a class=\"title \" href"))
        {
            raw = raw.Substring(raw.IndexOf("<a class=\"title \" href"));
            String link = raw.Substring(24, raw.IndexOf(">", 24) - 26);

            int startname = raw.IndexOf(">", 24) + 1;
            int endname = raw.IndexOf("</a>&#32;");
            String name = raw.Substring(startname, endname - startname);

            images.Add(new Tuple<string, string>(name, link));

            raw = raw.Substring(endname);
        }



}

I want to return 'images', but converted to a multidimensional array.

5
  • 1
    Can you give an example of what the Tuple looks like? I'm assuming Tuple<string, string>? Commented Dec 21, 2012 at 1:09
  • 1
    Can you give an example of input and output? What have you tried? Commented Dec 21, 2012 at 1:10
  • 2
    Honestly, what have you tried? Show us some code please. Commented Dec 21, 2012 at 1:10
  • A string[,] is a 2-dimensional array of strings, not just two parallel string arrays. What are you expecting the string[,] to contain? Are you expecting/intending one of the dimensions to only be of size 2? Also, most C# developers will see a Tuple<string,string> and understand that as a "pair of strings"... using a string[,] does not connote the same meaning. Commented Dec 21, 2012 at 1:21
  • It's not to late to use a proper html parser. Your code looks like it might break any second. HtmlAgilityPack is pretty nifty for this sort of thing. Commented Dec 21, 2012 at 1:22

1 Answer 1

3

A simple and straight-forward way is to just for the list:

string[,] result = new string[images.Count, 2];

for(int i=0; i<images.Count; i++)
{
  var tuple = images[i];
  result[i,0] = tuple.Item1;
  result[i,1] = tuple.Item2;
 }
 return result;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.