FrontPage

Praat Script Tips

Emulation of Perl's split function

string$ = "hoge/piyo/foo/bar"
delimiter$ = "/"

n = 0
arr_0$ = string$
repeat
    lastValue$ = arr_'n'$
    delimiterPos = index(lastValue$,delimiter$)
    if delimiterPos <> 0
        arr_'n'$ = left$(lastValue$,delimiterPos-1)
        n = n+1
        arr_'n'$ = right$(lastValue$,length(lastValue$)-delimiterPos-length(delimiter$)+1)
    endif
until delimiterPos = 0

clearinfo
for i from 0 to n
    tmp$ = arr_'i'$
    printline 'i''tab$''tmp$'
endfor
0	hoge
1	piyo
2	foo
3	bar

Get/Rewrite the Path Separator

input$ = "c:/foo/bar/hoge.TextGrid"
output$ = "c:/foo/bar/result"

if windows = 1
    pathSep$ = "\"
    notPathSep$ = "/"
elsif macintosh = 1 or unix = 1
    pathSep$ = "/"
    notPathSep$ = "\"
else
   exit "Sorry. Unknown platform."
endif
input$ = replace$(input$,notPathSep$,pathSep$,0)
output$ = replace$(output$,notPathSep$,pathSep$,0)

clearinfo
printline 'input$'
printline 'output$'
c:\foo\bar\hoge.TextGrid
c:\foo\bar\result

Get the path and filename

dir$ = "c:/hoge/piyo/foo.TextGrid"
#dir$ = "c:\hoge\piyo\foo.TextGrid"

if windows + macintosh + unix = 0
   exit "Sorry. Unknown platform."
endif

path$ = left$(dir$,rindex_regex(dir$,"[/\\]"))
filename$ = right$(dir$,length(dir$)-rindex_regex(dir$,"[/\\]"))
## if you have the path separator (pathSep$) already, you can write:
#path$ = left$(dir$,rindex(dir$,pathSep$))
#filename$ = right$(dir$,length(dir$)-rindex(dir$,pathSep$))

clearinfo
printline 'path$'
printline 'filename$'
c:/hoge/piyo/
foo.TextGrid

Upper to Lower Case

str$ = "ABCDEFG"

newstr$ = replace_regex$(str$,"[A-Z]","\L&",0)

echo 'newstr$'
abcdefg

Front page   New List of pages Search Recent changes   Help   RSS of recent changes