Search code examples
functionargumentspraat

Argument by value not being passed in Praat


I have made a procedure in Praat script which looks like this:

procedure segment_allowed: .segment$
    appendInfoLine: "The argument I got was: ", .segment$
    .yes = 0
    for i from 1 to allowed_segments_size
        if allowed_segments$[i] = .segment$
            .yes = 1
        endif
    endfor
endproc

It's basically trying to find out if .segment$ exists in a global array allowed_segments$.

The function when called like this:

call segment_allowed segment_label$

always outputs:

The argument I got was segment_label$

Why is the function/procedure not picking up the actual value and treating the variable like a string?


Solution

  • You are mixing the old syntax (the "shorthand") and the new syntax, and that's confusing things.

    When you write call segment_allowed segment_label$, you are using the "shorthand", and in that (deprecated) syntax style variables are not automatically interpolated. If you want to use that style, you should write

    call segment_allowed 'segment_label$'
    

    to force interpolation.

    A much better way to do it is to use the new syntax (which you are using in your procedure definition), which does a much more sensible variable interpolation. Using this new syntax (available from about version 5.4), your procedure call should be

    @segment_allowed: segment_label$
    

    which should do what you want.

    As an aside, translating your current procedure call into this new syntax, which is easier to understand, what you were calling was

    @segment_allowed: "segment_label$"
    

    Also note that from 1 in for loops is redundant, since that is the default. And if what you are interested is just knowing whether the segment is there or not, you could break from the loop when a match has been found, like this (I also changed your i for .i, to keep things tidy):

    procedure segment_allowed: .segment$
        appendInfoLine: "The argument I got was: ", .segment$
        .yes = 0
        for .i to allowed_segments_size
            if allowed_segments$[.i] = .segment$
                .yes = 1
                .i += allowed_segments_size
            endif
        endfor
    endproc