View Full Version : How to remove commas when printing out array values?
03-20-2009, 11:30 PM
Anyone knows how to remove commas from the output when using printAllElements to print out an array?
The file format I'm writing to needs to have clean values (space delimited) and gives errors on commas. Printing out array in one go gives me a huge performance increase, but commas ruin everything.
I'm on Max 9 btw. I tried to convert array to string and replace the commas inside string, but this is painfully slow with large arrays.
All ideas are welcome!
03-21-2009, 02:15 AM
how about:for val in myArray do format "% " val to:myOutputFile
03-21-2009, 10:09 AM
Thanks for the idea, but that would not work like i want, cause it would still loop trough an array and write out each element separately. I want to do format "%" array to:file, but without the commas. Probably not possible...
03-21-2009, 01:40 PM
Gravey's method is the appropriate method, I'm afraid.
You could hack something like formatting the array to a stringstream first, the removing the commas with subsituteString - but what if one of your elements itself is a string that has a comma?
What do you want the result to be, exactly, that Gravey's method doesn't provide?
03-21-2009, 01:45 PM
based off your response, an example of the result you are after would look like this:
#(1 2 3 4 5 6 7 8 9)
so the only difference between that and the result from my first post is the start #( characters and end ) character. so just format them before and after each array! or create a function to do so like this:fn formatArrayWithoutCommas arr myOutputFile =
format "#(" to:myOutputFile
for val in arr do format "% " val to:myOutputFile
format ")" to:myOutputFile
)another option would be to use the dotnet System.String replace method.
eg.myArray = #(1, 2, 3, 4, 5, 6)
arrayString = dotNetObject "System.String" (myArray as string)
outputString = arrayString.Replace "," "" -- replace commas with an empty string
-- result should be: "#(1 2 3 4 5 6)"
format outputString to:outputFile
03-22-2009, 10:41 AM
Awesome, thank you so much Gravey !
This dot net method was exactly what I needed. Other ways to format an array had loops and loops in maxscript are a bit slow. By writing out an array (string) to file all at once (not using any loops) gives quite a performance boost.
Thanks again ! It works perfectly.
Ok, it has a downsize. It works for mid-size arrays. If the array gets large, then the process gets slow. Probably cause the string is too large in memory. Oh well...
Found a workaround. I'm outputing the string in chunks.
So problem solved. Thanks again.
03-22-2009, 10:41 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.