List of list of strings

I have several paths, for example:

C:\TEMP\TEST1234123412341234.pdf
C:\TEMP\TEST1.pdf
C:\TEMP\TEST1234123412341234545.pdf

I would like to create one list composed of lists. Each sublist must contain a maximum of 50 characters, as below:

{C:\TEMP\TEST1234123412341234.pdf,C:\TEMP\TEST1.pdf},{C:\TEMP\TEST1234123412341234545.pdf}

Certainly! Here’s a step-by-step guide to implement the described logic in VB.NET:

  1. Initialize Variables:

    • Initialize a List of Lists to store the sublists of paths:
     sublists As New List(Of List(Of String))()
    
  2. Iterate Through Paths:

    • Use a loop (e.g., For Each) to iterate through your list of paths.
    • Inside the loop, check the length of the current path and add it to the last sublist in the sublists variable if the total length is less than or equal to 50 characters.
    • If adding the current path exceeds 50 characters, create a new sublist and add the path to it.
  currentSublist As New List(Of String)()

    For Each path As String In paths
        If currentSublist.Sum(Function(p) p.Length) + path.Length <= 50 Then
            currentSublist.Add(path)
      in   Else
            sublists.Add(currentSublist)
            currentSublist = New List(Of String) From {path}
        End
 If
    Next

    ' Add the last sublist to sublists (if not empty)
    If currentSublist.Count > 0 Then
        sublists.Add(currentSublist)
    End If
  1. Output the Result:
    • Use a suitable method to output the final sublists variable, which contains the lists of paths with a maximum of 50 characters each.

to display

For Each sublist As List(Of String) In sublists
Console.WriteLine(β€œ{” & String.Join(β€œ,”, sublist) & β€œ}”)

Adjust the code based on your specific needs, such as using a different output method or handling the sublists as needed.

2 Likes

Hi @PiotrekF

Assign maxCharacters = 50
Assign inputPaths = {"C:\TEMP\TEST1234123412341234.pdf", "C:\TEMP\TEST1.pdf", "C:\TEMP\TEST1234123412341234545.pdf"}
Assign mainList = New List(Of List(Of String))

Assign currentList = New List(Of String)
For Each path In inputPaths
    If (currentList.Sum(Function(x) x.Length) + path.Length) <= maxCharacters Then
        currentList.Add(path)
    Else
        mainList.Add(currentList)
        currentList = New List(Of String)
        currentList.Add(path)
    End If
Next

If currentList.Count > 0 Then
    mainList.Add(currentList)
End If


Output is in Mainlist.

1 Like

Hello @PiotrekF

Assign activity:
Input: yourFilePaths (an array or list containing your file paths)
To: fileList

Assign activity:
Input: 50
To: maxLength

Assign activity:
Input: fileList.Select(Function(s, i) New With {Str = s, Index = i}).GroupBy(Function(x) x.Index \ (maxLength + 1)).Select(Function(g) g.Select(Function(x) x.Str).ToList()).ToList()
To: groupedLists

Thanks & Cheers!!!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.