I have code that assigns section numbers to notes within a container: 1, 1.1, 1.2, 2, 2.1,2.2, 2.2.1, etc.
This is working well.
For better sorting I sometimes want to pad with leading “zeros”, e.g. 01, 01.01, 01.02, 02, 02.01, 02.02, 02.02.01 etc.
I would like to put this in a function.
I thought I had this going well until I ran across the problem illustrated in the attached tbx.
Test padlist.tbx (133.1 KB)
This is my padding function:
function padlist(aList:list,decimals,width,padchar:string){
// pad section numbers for better sorting
var:list result;
aList.each(anItem){
result += anItem.format(decimals, width, padchar);
};
return result;
}
It works as expected when the list has more than one item. But when the note is top-level, i.e. the list of sibling orders has only one item, the leading 0 is dropped. Judging by .size, in all cases the function seems to return a string rather than a list.
Similar code not placed in a function gives the desired results, i.e. it does not drop the leading 0(s) when there is only one item, and list.size is as I would expect.
In the test tbx, I have two stamps, one calling the function and one not. The output goes to the text of the Log note.
I directly edit the list in each stamp to test for one item only.
What am I doing wrong in the function? Or is this an anomaly?
(Unrelated but discovered along the way: list.slice() perhaps deserves an entry in aTbRef if it is not already there-I couldn’t find it and for a while assumed Tinderbox didn’t have it.)






