MiniM. Saving time.

| About | Download | Tools | Knowledge Base | How to Buy |

MiniM Overview

Getting Started




Cache Tools

GT.M Tools

Knowledge Base




MiniM Knowledge Base | Oct 12, 2020

Convert nested lists to a string

Some mumps have list conversion commands. for example, $listtostring, unfortunately this command converts only a linear list (single-level) and for nested (multi-level) lists, it is not suitable.

The implementation of m-code conversion of nested lists into a string is proposed. This ņonverter is suitable for applications where lists are often converted to text structures, such as"json".

Replacing the built-in $listtostring function with the specified m-code (routine ^listtostr):

listtostr(list) ;$$; convert lists2lists to string {,1,2,{3,4},...}
 n s,ss,i,n
 s s=""
 i $lv(list) d
 . s s=s_"{",n=$ll(list)
 . f i=1:1:n s ss=$lg(list,i,""),s=s_$$listtostr(ss)_$s(i<n:",",1:"")
 . s s=s_"}"
 . i s="{}" s s=""
 e s s=s_list
 q s
Example of using $$^listtostr:
USER>s list=$lb(,1,2,$lb(3,4,$lb(5,6),7,8),9,10,)
USER>s slist=$$^listtostr(list)
USER>w !,"slist=",slist

Alexander Chudnovsky

Copyright (C) Eugene Karataev
Info Support