Sign in to follow this  

MaxScript error when trying to operate on strings

This topic is 2384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I am writing an exporter in MaxScript (using 2011) and am having trouble with a function thats meant to clean spaces from strings:

[code]function CleanSpaces str_in
=(
str_out = (copy str_in as string)

for i = 1 to (str_out.count as Integer) do
(
if(str_out[i] == " ")then
(
str_out[i] = "_"
)
)

str_out
)
[/code]


When I copy this into the listener and enter any string I recieve the following:

[code]cleanspaces "example string"
-- Error occurred in i loop
-- Frame:
-- i: 1
-- called in CleanSpaces()
-- Frame:
-- str_out: "example string"
-- str_in: "example string"
-- Type error: Call needs function or class, got: undefined[/code]

I cannot see any problem with this simple function, is there anyone familiar with MaxScript who could tell me whats wrong?

Thanks!

Share this post


Link to post
Share on other sites
Just tested with Max 9, got no error. It converted "bla bli ble" to "bla_bli_ble".

But in any case I would do this differently; there are some things in there that you could optimize.

[code]
 -- A side effect of this implementation is that it trims spaces at the start or end.

function CleanSpaces str_in =(

    local temp = filterString str_in " "
    local amount = temp.count - 1 -- Precalculate the amount of tokens so as not to keep evaluating ".count" every iteration.
    local str_out = ""

    for i in 1 to amount do (
        str_out += (temp[i] + "_")
    )

    (str_out + temp[temp.count])
)
 [/code]

Share this post


Link to post
Share on other sites
Hi again, now with more time I could think better. You see, in that function I posted it would remove consecutive spaces and that might not be what you're after: something with five spaces between words would have them replaced with just one. It wouldn't be consistent with the input.

So the original function gives a better result. With that in mind, we can try other ways to optimize it (just for the heck of it; also because you're writing an exporter and need all the nanoseconds you can get).

Here is a little speed test with two different versions: the original and one that uses the 'Where' expression instead of an 'If'.
[code]
function CleanSpaces1 str_in = ( -- ORIGINAL
    str_out = (copy str_in as string)

    for i = 1 to (str_out.count as Integer) do
    (
        if(str_out[i] == " ") do -- Changed to 'do'.
        (
            str_out[i] = "_"
        )
    )

    str_out
)

function CleanSpaces2 str_in = ( -- USING 'WHERE'
    local str_out = copy (str_in as string)
    size = str_in.count

    for i = 1 to size where (str_out[i] == " ") do (
        str_out[i] = "_"
    )

    str_out
)

start = timeStamp()
for i = 1 to 1500 do (
    CleanSpaces1 "foo bar      is nice."
)
format "Time taken for original: %" (timeStamp() - start)

start = timeStamp()
for i = 1 to 1500 do (
    CleanSpaces2 "foo bar      is nice."
)
format "Time taken for 'Where' version: %" (timeStamp() - start)
[/code]
Everytime I evaluated (CTRL+E) the above, the "Where" version ran a few milliseconds faster.
Also, "If ... Then" expressions are slower than "If ... Do" ones (because they allow an "Else" to be added). So any time you need an "If" that doesn't require an "Else", use a "Do" for these single block conditions. 

While there are a few MaxScript threads here, I can't recommend enough the CGSociety forums where they have  [url="http://forums.cgsociety.org/forumdisplay.php?f=98"]a whole subforum[/url] for the MaxScript language. Plenty of technical artists over there, it's not only a great place to ask questions but to read the discussions as well.
Make sure to read the  [url="http://www.kxcad.net/autodesk/Autodesk_MAXScript_Reference_9/How_To_Make_It_Faster_.htm"]How To Make It Faster?[/url] article that comes with the MaxScript documentation, it explains several good practices and some very effective optimizations. 

Share this post


Link to post
Share on other sites

This topic is 2384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this