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.

Community
  • 1
  • 1
Wilson
  • 8,570
  • 20
  • 66
  • 101
  • 1
    Can you give an example of what the Tuple looks like? I'm assuming `Tuple`? – Simon Whitehead Dec 21 '12 at 01:09
  • 1
    Can you give an example of input and output? What have you tried? – SWeko Dec 21 '12 at 01:10
  • 2
    Honestly, what have you tried? Show us some code please. – Kobi Dec 21 '12 at 01: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` and understand that as a "pair of strings"... using a `string[,]` does not connote the same meaning. – JaredReisinger Dec 21 '12 at 01: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. – spender Dec 21 '12 at 01:22

1 Answers1

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;
SWeko
  • 30,434
  • 10
  • 71
  • 106