Notifications
Clear all

[Closed] Removing the last character from a formatted list

Here’s the problem: I’m ouputting some coordinates to a file with

function printPt vert = (format "%," vert to:outfile)

format "
  \"vertices\" : ["  to:outfile
			for facenum = 1 to total_faces do (
				faceIndex = getFace myObj facenum
					for vIndex = 1 to 3 do (
					   vertex = getVert myObj faceIndex[vIndex] 
					   printPt vertex.x
					   printPt vertex.y
					   printPt vertex.z					
					)
			)

The problem is that at the end of a list I get an unwanted column “,”
Is there a way to workaround that? Something like “delete last char”?

3 Replies

try this, moves the current file read/write point back one.

seek outfile ((filePos outfile) - 1);
 format "]"  to:outfile;

Yeeees. Works perfectly. Thanks Klunk

your format is a little strange for me. are you really want to print all faces vertices positions in one line? it’s very hard for manually (visually) debug

i would suggest something like:

(	
	delete objects 
	b = box()
	mesh = b.mesh			
	ss = stringstream ""			
	for f=1 to mesh.numfaces do
	(
		vv = getface mesh f
		format "%: % % %
" vv (getvert mesh vv[1]) (getvert mesh vv[2]) (getvert mesh vv[3]) to:ss
	)
	ss as string
)

it’s visually simple and easy to parse:

one line per face

vertex indices the first point3 separated from positions by “:”

next three point3s are correspondent positions separated by ” “(space)