locked
LIST - How to form a list in the given example RRS feed

  • Question

  • User-1024101449 posted

    Hi,

    I want to form below output after executing my code.

    Output should come as mentioned below format as list <string> RESULTVALUE .

    list <string> RESULTVALUE 

    [0]Sub1 = 400

    [1]Sub2 = 500

    i'm struggling to form the loop

    below is my code

      List<string> RESULTVALUE  = new List<string>();
            string str1 = "Sub1#Sub2";
            string[] words1 = str1.Split('#');   
            string str2 = "400,500";
            string[] words2 = str2.Split(',');
            double value = 0;

            for (int i = 0; i < words1.Length; i++) 
            {
                for (int j = 0; j < words2.Length; i++) 
                {

                    value = Convert.ToDouble(words2[j]);  ????????
                }

                RESULTVALUE.Add(words1[i] + "=" + value); ????????
                
            }

    Final output is :

    list <string> RESULTVALUE 

    [0]Sub1 = 400

    [1]Sub2 = 500

    where we need to change the code..?

    Friday, July 21, 2017 7:05 AM

Answers

  • User-335504541 posted

    Hi gani7787,

    Does the length of words1 always equal the length of words2?

    If so you could try below code:

                List<string> RESULTVALUE = new List<string>();
                string str1 = "Sub1#Sub2";
                string[] words1 = str1.Split('#');
                string str2 = "400,500";
                string[] words2 = str2.Split(',');
                 double value = 0;
    
                for (int i = 0; i < words1.Length; i++)
                {  
                    value = Convert.ToDouble(words2[i]);              
    
                    RESULTVALUE.Add(words1[i] + "=" + value);
                 }

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 21, 2017 9:29 AM

All replies

  • User-335504541 posted

    Hi gani7787,

    Does the length of words1 always equal the length of words2?

    If so you could try below code:

                List<string> RESULTVALUE = new List<string>();
                string str1 = "Sub1#Sub2";
                string[] words1 = str1.Split('#');
                string str2 = "400,500";
                string[] words2 = str2.Split(',');
                 double value = 0;
    
                for (int i = 0; i < words1.Length; i++)
                {  
                    value = Convert.ToDouble(words2[i]);              
    
                    RESULTVALUE.Add(words1[i] + "=" + value);
                 }

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 21, 2017 9:29 AM
  • User1551896424 posted

    Hi 

    gani7787,

    If I am not wrong then what I understood is, you want to parse two lists and want to concatenate into one with pairing values from both the list. To achieve this you can you Dictionary as below. Also assuming length of both of the lists will be same. Or you need to apply extra validation.

    string str1 = "Sub1#Sub2";
    string[] words1 = str1.Split('#');
    string str2 = "400,500";
    string[] words2 = str2.Split(',');	
    	
    Dictionary<string, string> RESULTVALUE = new Dictionary<string, string>();
    for(int i = 0 ; i < words1.Length ; i++)
    {
    	RESULTVALUE.Add(words1[i], words2[i]);
    }
    	
    foreach (var pair in RESULTVALUE)
    {
          Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
    }

    Output:

    Sub1, 400
    Sub2, 500

    Thursday, August 3, 2017 11:22 AM
  • User303363814 posted

    Or with a bit less code ....

    string str1 = "Sub1#Sub2";
    string str2 = "400,500";
    var RESULTVALUE = Enumerable.Zip(str1.Split('#'), str2.Split(','), (first, second) => $"{first}={second}");
    

    Friday, August 4, 2017 6:42 AM