Question
· May 12, 2023

Recommended way to split a list and generate a string structure

Good morning,

I was wondering:

Given the following scenario where we have a string where each two items are being splitted by "|" as follows: "squadName|initialLetter"

"Alfa|A|Bravo|B|Charlie|C|Delta|D|Echo|E|Foxtrot|F|Golf|G|Hotel|H|India|I|Juliett|J|Kilo|K|Lima|L|Mike|M|November|N|Oscar|O|Papa|P|Quebec|Q|Romeo|R|Sierra|S|Tango|T|Uniform|U|Victor|V|Whiskey|W|X-ray|X|Yankee|Y|Zulu|Z"

 

And we would need to generate a String structure like:
 

[
    {
        "number": "number 1",
        "squad": "Alfa",
        "answer": [
            {
                "initialLetter": "A"
            }
        ]
    },

[...]

{
        "number": "number 26",
        "squad": "Zulu",
        "answer": [
            {
                "initialLetter": "Z"
            }
        ]
    }
]

How would you propose to do that functionality?

 

 

 

 

 

 

 

 

 

I would suggest:

ClassMethod createSquads(stringContent As %String(MAXLEN="")) As %Library.DynamicArray
{
    ;1.- Generate list by pipes "|"
    SET list 			= $LISTFROMSTRING(stringContent, "|")
    set items 			= []
    set numberSquad 	= 0
    
    ;2.- Iterate
    FOR i=1:2:$LISTLENGTH(list) {
        ;3.- Update variables
        set numberSquad 	= numberSquad + 1
        SET squad 			= $LISTGET(list, i)
        SET initial 		= $LISTGET(list, i+1)
        ;4.- Generate item 
        set item 			= 									
        {
        "number": ("number "_(numberSquad)),
        "squad": (squad),
        "answer": 
        [
            {
                "initialLetter": (initial)
            }
        ]
        }
        do items.%Push(item)
    }	
    quit items
}

 

We would call it as follows:

w ##class(className).createSquads("Alfa|A|Bravo|B|Charlie|C|Delta|D|Echo|E|Foxtrot|F|Golf|G|Hotel|H|India|I|Juliett|J|Kilo|K|Lima|L|Mike|M|November|N|Oscar|O|Papa|P|Quebec|Q|Romeo|R|Sierra|S|Tango|T|Uniform|U|Victor|V|Whiskey|W|X-ray|X|Yankee|Y|Zulu|Z").%ToJSON()

 

It would generate:

[
	{
		"number": "number 1",
		"squad": "Alfa",
		"answer": [
			{
				"initialLetter": "A"
			}
		]
	},
	{
		"number": "number 2",
		"squad": "Bravo",
		"answer": [
			{
				"initialLetter": "B"
			}
		]
	},
	{
		"number": "number 3",
		"squad": "Charlie",
		"answer": [
			{
				"initialLetter": "C"
			}
		]
	},
	{
		"number": "number 4",
		"squad": "Delta",
		"answer": [
			{
				"initialLetter": "D"
			}
		]
	},
	{
		"number": "number 5",
		"squad": "Echo",
		"answer": [
			{
				"initialLetter": "E"
			}
		]
	},
	{
		"number": "number 6",
		"squad": "Foxtrot",
		"answer": [
			{
				"initialLetter": "F"
			}
		]
	},
	{
		"number": "number 7",
		"squad": "Golf",
		"answer": [
			{
				"initialLetter": "G"
			}
		]
	},
	{
		"number": "number 8",
		"squad": "Hotel",
		"answer": [
			{
				"initialLetter": "H"
			}
		]
	},
	{
		"number": "number 9",
		"squad": "India",
		"answer": [
			{
				"initialLetter": "I"
			}
		]
	},
	{
		"number": "number 10",
		"squad": "Juliett",
		"answer": [
			{
				"initialLetter": "J"
			}
		]
	},
	{
		"number": "number 11",
		"squad": "Kilo",
		"answer": [
			{
				"initialLetter": "K"
			}
		]
	},
	{
		"number": "number 12",
		"squad": "Lima",
		"answer": [
			{
				"initialLetter": "L"
			}
		]
	},
	{
		"number": "number 13",
		"squad": "Mike",
		"answer": [
			{
				"initialLetter": "M"
			}
		]
	},
	{
		"number": "number 14",
		"squad": "November",
		"answer": [
			{
				"initialLetter": "N"
			}
		]
	},
	{
		"number": "number 15",
		"squad": "Oscar",
		"answer": [
			{
				"initialLetter": "O"
			}
		]
	},
	{
		"number": "number 16",
		"squad": "Papa",
		"answer": [
			{
				"initialLetter": "P"
			}
		]
	},
	{
		"number": "number 17",
		"squad": "Quebec",
		"answer": [
			{
				"initialLetter": "Q"
			}
		]
	},
	{
		"number": "number 18",
		"squad": "Romeo",
		"answer": [
			{
				"initialLetter": "R"
			}
		]
	},
	{
		"number": "number 19",
		"squad": "Sierra",
		"answer": [
			{
				"initialLetter": "S"
			}
		]
	},
	{
		"number": "number 20",
		"squad": "Tango",
		"answer": [
			{
				"initialLetter": "T"
			}
		]
	},
	{
		"number": "number 21",
		"squad": "Uniform",
		"answer": [
			{
				"initialLetter": "U"
			}
		]
	},
	{
		"number": "number 22",
		"squad": "Victor",
		"answer": [
			{
				"initialLetter": "V"
			}
		]
	},
	{
		"number": "number 23",
		"squad": "Whiskey",
		"answer": [
			{
				"initialLetter": "W"
			}
		]
	},
	{
		"number": "number 24",
		"squad": "X-ray",
		"answer": [
			{
				"initialLetter": "X"
			}
		]
	},
	{
		"number": "number 25",
		"squad": "Yankee",
		"answer": [
			{
				"initialLetter": "Y"
			}
		]
	},
	{
		"number": "number 26",
		"squad": "Zulu",
		"answer": [
			{
				"initialLetter": "Z"
			}
		]
	}
]

How would you code this functionality, in a more readable and understandable way?

Thanks for reading and thanks for replying.

Greetings.

Discussion (4)2
Log in or sign up to continue

I approached in similar way.

Difference was to use the built in "$Piece" function on original string, instead of using intermediary lists.

Also using the dynamic property names.

   ClassMethod AsJSON(value = "")
{
set dyn=[]
set len=$L(value,"|")
if len>1 {
for i=1:2:len {
set q=$P(value,"|",i)
set a=$P(value,"|",i+1)
set item={}
set item.number="number "_(i\2+1)
set item.squad=q
set item.answer=[]
set answer1={}
set answer1.initialLetter=a
do item.answer.%Push(answer1)
do dyn.%Push(item)
}
}
quit dyn.%ToJSON()
}

Thanks Alexander Koblov,

Why is $Listnext recommended?

Is it true?

The "$List" function returns a complete list, while "$ListGet" returns a specific item from a list given its index. When you need to iterate over a list, you can use the "$Listnext" function. This function allows you to get the elements of a list one by one, without having to load the whole list into memory at once.

Using "$Listnext" instead of "$List" or "$ListGet" can improve performance, as it avoids the need to load and process the entire list at once. Instead, each item can be fetched individually as needed, which is particularly useful in cases where the list is large.