1

I have an ArrayList with multiples items on it, everyone of them is a String divided by commas "loglogs", the three first items are the localization (Destin, lat and long). I need to insert the Strings of these loglogs in buttons depending on its localization (based on that three parameters) in the button Tooltip or text programatically. I have all the button creation but I have to add the strings but there are more loglogs than buttons so...

I need to "filter" the ArrayList into another ArrayList, filter it depending on these three inital coordinates, I want to create another ArrayList but appending the strings that are identical in their three first elements of the arrayList. That way I will combine the "loglogs" into another "loglogsCondensed", with all the "localization" unique so I can add this part to my button and index creation.

        foreach (String log in logslogs)
        {
            String[] colContent = log.Split(',');   //splited the content with commas
            Loglog log = new Loglog();  //Loglog is a class of logs with information in specific columns
            log.Destin = colContent[0];
            log.Lat = Convert.ToChar(colContent[1]);
            log.Long = colContent[2];                
            log.Barcode = colContent[6];
            log.Source = colContent[7];                
            log.SampleName = colContent[9];                
            AllLogs.Add(log);

I need to pass from logslogs with 1000 memebers to an ArrayList with less items, where the ones with the same location based on the three first items are appended as one item.

Suposse this is kind of easy if you know how to code properly (not my case). A thousand thanks only for read this out, even more to the people who try to help.

Best,

0

1 Answer 1

0

I have the solution!, probably is not going to win any contest of cleaneness but it does what I need!. I create an index to filter comparing the items depending of the three coordinates: Destin, Long and Lat. If they are the same I remove the last item and put the appended line in the last place and so on...

    int c = 0;  //Just to go the first time
    //We create an index to compare the former with the "actual"     
    //log in every loop of the "foreach"

        String IndiceDestin0 = string.Empty;
        String IndiceLat0 = string.Empty;
        String IndiceLong0 = string.Empty;

        String IndiceDestin1;
        String IndiceLat1;
        String IndiceLong1;          

        foreach (String log in logslogs)
        {
            String[] LongContent = log.Split(',');
            Loglog log = new Loglog();
            log.Destin = LongContent[0];
            log.Lat = Convert.ToChar(LongContent[1]);
            log.Long = LongContent[2];                
            log.Barcode = LongContent[6];
            log.Source = LongContent[7];
            log.DestDestinBarcode = LongContent[8];
            log.SampleName = LongContent[9];                
            AllLogs.Add(log);

            //This only works once, the first time because we don't have a "former" data to compare we have to bypass the comparison
            if (c == 0) 
            {
                IndiceDestin0 = LongContent[0];
                IndiceLat0 = LongContent[1];
                IndiceLong0 = LongContent[2];
                c++;
            }
            else
            {
                IndiceDestin1 = LongContent[0];
                IndiceLat1 = LongContent[1];
                IndiceLong1 = LongContent[2];

                if (IndiceDestin0.Equals(IndiceDestin1) && IndiceLat0.Equals(IndiceLat1) && IndiceLong0.Equals(IndiceLong1))
                {                        
                    int last = logsToButtons.Count - 1;
                    string oldLog = logsToButtons[last].ToString();
                    string appendedLog = oldLog + log;
                    //We remove the last "single" log to add the aggregated log
                    logsToButtons.RemoveAt(last);

                    logsToButtons.Add(appendedLog);                        
                }
                else
                {
                    logsToButtons.Add(log);
                }

                IndiceDestin0 = IndiceDestin1;
                IndiceLat0 = IndiceLat1;
                IndiceLong0 = IndiceLong1;
                c++;
            }                               

        }

I get to have a shorter version of the array but appending together the ones that have the same coordenates, thank you everybody for your help, I know is messy but it works!

Best,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.